我正在使用 react-rails gem,我正尝试在 ES6 中编写一些如下所示的组件。
我的 link_list.js.jsx 文件
import Component from 'react';
import Links from 'link';
class LinkList extends React.component{
constructor(props){
super(props);
this.sate = {};
}
getInitialState(){
return { links: this.props.initialLinks}
}
render(){
var links = this.state.links.map(function(link){
return <Links key={link.id} link={link} />
})
return (
<div className="links">
{links}
</div>
)
}
}
我一直收到这个 Uncaught ReferenceError: require is not defined
和一个错误提示 Warning: React.createElement: type should not be null, undefined, boolean, or number.它应该是一个字符串(对于 DOM 元素)或一个 ReactClass(对于复合组件)。
和错误 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
这是我的代码的问题还是 gem 没有正确编译 ES6 的问题?
最佳答案
生成命令中有一个选项
rails 生成 react:component 标签 label:string --es6
https://github.com/reactjs/react-rails#options
否则,您可以使用 webpack
设置前端并使用 babel
。
关于javascript - Rails-React 不适用于 ES6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36088097/