记住这些代码:
var Component = React.createClass({
getInitialState: function () {
return {position: 0};
},
componentDidMount: function () {
setTimeout(this.setState({position: 1}), 3000);
},
render: function () {
return (
<div className="component">
{this.state.position}
</div>
);
}
});
ReactDOM.render(
<Component />,
document.getElementById('main')
);
状态不是应该在3秒后才改变吗? 它马上就改变了。
我在这里的主要目标是每3秒改变状态(使用 setInterval()
) ,但由于它不工作,我尝试了 setTimeout()
,这也不工作。有灯光吗?谢谢!