当我运行它时,它给我一个错误:
Uncaught SyntaxError: 'super' keyword unexpected here
var React = require("react");
module.exports = React.createClass({
constructor(){
super();
this.state = {date: new Date()};
},
render() {
return(
<div>
test
</div>
);
}
});
PS。我删除了 super()
它给了我一个错误
You are attempting to define
constructor
on your component more than once. This conflict may be due to a mixin.
示例来自React Docs
我做错了什么?
最佳答案
如果您不使用 ES6 类,而是使用 React.createClass()
语法,请通过 getInitialState()
设置初始状态,而不是通过构造函数:
var Counter = React.createClass({
getInitialState: function() {
return {count: this.props.initialCount};
},
// ...
});
<小时/>
也就是说,最好的做法是切换到 ES6 类。这样您就可以使用文档中的构造函数语法。
关于javascript - 语法错误: 'super' keyword unexpected here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42208921/