Uncaught ReferenceError: React is not defined
I am trying to make ReactJS work with rails using this tutorial. I am getting this error:
Uncaught ReferenceError: React is not defined
But I can access the React object in browser console
I also added public/dist/turbo-react.min.js as described here and also added //= require components
line in application.js as described in this answer to no luck. Additionally,var React = require('react')
gives the error:Uncaught ReferenceError: require is not defined
Can anyone suggest me on how to resolve this?
[EDIT 1]
Source code for reference:
This is my comments.js.jsx
file:
var Comment = React.createClass({render: function () {
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
{this.props.comment}
</div>
);
}
});
var ready = function () {
React.renderComponent(
<Comment author="Richard" comment="This is a comment "/>,
document.getElementById('comments')
);
};
$(document).ready(ready);
And this is my index.html.erb
:
<div id="comments"></div>
Answer
I was able to reproduce this error when I was using webpack to build my javascript with the following chunk in my webpack.config.json
:
externals: { 'react': 'React'
},
This above configuration tells webpack to not resolve require('react')
by loading an npm module, but instead to expect a global variable (i.e. on the window
object) called React
. The solution is to either remove this piece of configuration (so React will be bundled with your javascript) or load the React framework externally before this file is executed (so that window.React
exists).
以上是 Uncaught ReferenceError: React is not defined 的全部内容, 来源链接: utcz.com/a/24691.html