简体   繁体   English

ReactDom没有使用react和webpack定义

[英]ReactDom is not defined with react and webpack

I've followed this straightforward tutorial ( http://jslog.com/2014/10/02/react-with-webpack-part-1/ ) for getting started with React and webpack but run into issues when trying to use more updated syntax. 我已经按照这个简单的教程( http://jslog.com/2014/10/02/react-with-webpack-part-1/ )开始使用React和webpack,但在尝试使用更新时会遇到问题句法。 ie using ReactDom to call render instead of the deprecated 'React.renderComponent'. 即使用ReactDom调用render而不是弃用的'React.renderComponent'。

I've run: 我跑了:

npm install --save react-dom

and added 并补充说

var ReactDom = require('react-dom')

to the index.jsx file and added 到index.jsx文件并添加

'react-dom': 'ReactDom'

to the list of 'externals' in webpack.config.js. 到webpack.config.js中的'externals'列表。 (I'm not sure if this was necessary.) (我不确定这是否必要。)

However I am getting the javascript error ReactDom is not defined. 但是我收到的javascript错误没有定义ReactDom。

webpack.config.js: webpack.config.js:

module.exports = {
entry: './index.jsx',
output: {
    filename: 'bundle.js', //this is the default name, so you can skip it
    //at this directory our bundle file will be available
    //make sure port 8090 is used when launching webpack-dev-server
    publicPath: 'http://localhost:8090/assets'
},
module: {
    loaders: [
        {
            //tell webpack to use jsx-loader for all *.jsx files
            test: /\.jsx$/,
            loader: 'jsx-loader?insertPragma=React.DOM&harmony'
        }
    ]
},
externals: {
    //don't bundle the 'react' npm package with our bundle.js
    //but get it from a global 'React' variable
    'react': 'React'
},
resolve: {
    extensions: ['', '.js', '.jsx']
}
}

If that is an exact copy of your code, then I think you referenced ReactDOM incorrectly - all the letters in DOM need to be capitalized, like so: 如果这是您的代码的精确副本,那么我认为您错误地引用了ReactDOM - DOM中的所有字母都需要大写,如下所示:

var ReactDOM = require('react-dom');

And in your webpack.config.js externals: 在你的webpack.config.js外部:

'react-dom': 'ReactDOM'

Note that when setting externals, Webpack expects you to load those libraries separately. 请注意,在设置外部时,Webpack希望您单独加载这些库。 For example, by putting a script tag in your HTML (index.html) pointing to the CDN's to react and react-dom or to the internal copies of those two libraries. 例如,通过在HTML(index.html)中放置一个脚本标记,指向CDN的响应和反应,或者对这两个库的内部副本进行反应。 So, that could also be why you are having issues. 所以,这也可能是你遇到问题的原因。

If you want to use the version of React and ReactDOM that came with your node modules, leave out the externals setting for Webpack. 如果要使用节点模块附带的React和ReactDOM版本,请省略Webpack的外部设置。 It will bundle React and ReactDOM with your own script (making the bundle.js longer), but it will work. 它会将React和ReactDOM与您自己的脚本捆绑在一起(使bundle.js更长),但它会起作用。 From what I can tell, there are a few other ways of handling this (like using Express to expose the relevant files from your node modules), but it seems many people simply bundle them together. 据我所知,还有一些其他方法可以解决这个问题(例如使用Express来显示节点模块中的相关文件),但似乎很多人只是将它们捆绑在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM