gpt4 book ai didi

javascript - 语法错误: 'super' keyword unexpected here

转载 作者:太空宇宙 更新时间:2023-11-04 16:01:49 26 4
gpt4 key购买 nike

当我运行它时,它给我一个错误:

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};
},
// ...
});

More info in the React docs

<小时/>

也就是说,最好的做法是切换到 ES6 类。这样您就可以使用文档中的构造函数语法。

关于javascript - 语法错误: 'super' keyword unexpected here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42208921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com