我希望我的小项目中尽可能地使用ES6(ES2015)。现在我想要在React中使用箭头函数。
// What I want
let Text = React.createClass({
componentDidMount: () => {
setInterval(this.updateCurrentTime, 1000);
}
}
// What I have
let Paragraph = React.createClass({
render: () => (<div className="MySuperTable"><Text name="Dodo" startDate={(new Date()).toString()} /></div>)
});
let Text = React.createClass({
getInitialState: function () {
return {
currentTime: (new Date()).toString()
}
},
componentDidMount: function () {
setInterval(this.updateCurrentTime, 1000);
},
updateCurrentTime: function () {
this.setState({
currentTime: (new Date()).toString()
});
},
render: function () {
return (
<div>
<span>Hello my name is {this.props.name}</span>
<span>And I was born on {this.props.startDate}</span>
<span>And I now it's {this.state.currentTime}</span>
</div>
);
}
});
ReactDOM.render(
<Paragraph/>,
document.getElementById('container')
);
- 我需要做什么才能让它工作?
- 据我所知,
this
将是传递给createClass
的对象本身,这正确吗? - 我该如何将它绑定到
Text
实例上?
提前致谢。