我練習反應
我遇到了這個錯誤:Uncaught ReferenceError: ReactDOM is not defined
當鉻控制檯類型ReactDOM.unmountComponentAtNode(document.body)
ReactDOM.unmountComponentAtNode:未捕獲的ReferenceError:ReactDOM沒有定義
請幫我查一下這個問題
我試穿的代碼JSbin和運行良好,所以我認爲這是webpack的問題,但我不知道。
而我注意到有很多方法寫React.render
部分當我谷歌,有什麼區別?哪一個是正確的?
React.render(<App name='Vipul' />,document.body);
ReactDOM.render(<App name='Vipul' />,document.body);
React.renderComponents(<App name='Vipul' />,document.body);
這裏是我的代碼:
main.jsx
import React from 'react';
import ReactDOM from 'react-dom';
console.log('Start')
var App = React.createClass({
render: function(){
console.log('render');
return <h1 onClick={this.toggleState}>Hello</h1>
},
componentWillUnmount: function(){
//在console執行 ReactDOM.unmountComponentAtNode(document.body)
console.log('componentWillUnmount');
},
toggleState: function(){
this.setState({status: !this.state.status})
}
});
ReactDOM.render(<App name='Vipul' />,document.body);
webpack.config.js
var WebpackNotifierPlugin = require('webpack-notifier');
module.exports = {
entry: "./src/main.js",
output: {
filename: "./dist/bundle.js"
// filename: "./public/dist/bundle.js"
},
plugins: [
new WebpackNotifierPlugin()
],
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: ['es2015', 'react']
}
}
]
},devtool: 'source-map'
};
謝謝。我明白了你的觀點。如果我在我的代碼中使用'setTimeout(()=> {0} {} {}}。它運作良好!謝謝 – user2492364