我正在尝试使用这个教程将ReactJS与rails结合使用。但是我遇到了以下错误:
Uncaught ReferenceError: React is not defined
但我可以在浏览器控制台中访问React对象。
我还按照此处所述添加了public/dist/turbo-react.min.js,并在application.js中添加了//= require components
行,如此答案所述,但没有运气。另外,
var React = require('react')
会报错:
Uncaught ReferenceError: require is not defined
有人能建议我如何解决吗?
[编辑1]
参考源代码:
这是我的comments.js.jsx
文件:
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);
这是我的index.html.erb
:
<div id="comments"></div>
import React from 'react'
。 - Nir Alfasi