Reactjs React构造器不工作
考虑一下这个类Reactjs React构造器不工作,reactjs,Reactjs,考虑一下这个类 var sentence = "The FBI has managed to unlock the iPhone"; define(["./UserInput","./Statistic","./RandomWords","react", "react-dom"], (UserInput,Statistic,RandomWords,React, ReactDOM) => { var Root = React.createClass({ c
var sentence = "The FBI has managed to unlock the iPhone";
define(["./UserInput","./Statistic","./RandomWords","react", "react-dom"],
(UserInput,Statistic,RandomWords,React, ReactDOM) => {
var Root = React.createClass({
constructor() {
super();
console.log("Hey");
this.state={sentenct: sentence }
}
render: function() {
return (
<div >
<h1>Welcome to our game</h1>
<UserInput/>
<RandomWords NewWord={ sentence }/>
<Statistic speed={"Salman"} success={"saba"} />
</div>
);
}
});
ReactDOM.render(
React.createElement(Root, null),
document.getElementById('content')
);
});
我试过了,而且
我制作了一个costructor,我希望在我的控制台中看到Hey,但它没有发生?您必须使用this.state.句子而不是变量
var sentence = "The FBI has managed to unlock the iPhone";
render: function() {
return (
<div >
<h1>Welcome to our game</h1>
<UserInput/>
<RandomWords NewWord={ this.state.sentence }/>
<Statistic speed={"Salman"} success={"saba"} />
</div>
);
}
如果问题仍然存在,请在下面的注释部分中写下,愿意调查由React.createClass创建的类没有构造函数方法。
只有ES6类具有构造函数方法
例如:
class Root extends React.Component {
constructor() {
super();
console.log("Hey");
this.state={sentenct: sentence }
}
}
你为什么不使用ES6呢?
class Root extends React.Component {
constructor() {
super();
console.log("Hey");
this.state={sentenct: sentence }
}
}