ReactJS:setTimeout()不起作用? [英] ReactJS: setTimeout() not working?
本文介绍了ReactJS:setTimeout()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
记住这些代码:
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秒后改变?它正在迅速改变。
Isn't the state supposed to change only after 3 seconds? It's changing immediately.
我的主要目标是每3秒更改一次状态(使用 setInterval()
),但是因为它我没有工作,我尝试了 setTimeout()
,这也没有用。这有什么灯吗?谢谢!
My main goal here is to change the state every 3 seconds (with setInterval()
), but since it was not working, I tried setTimeout()
, which is not working either. Any lights on this? Thanks!
推荐答案
执行
setTimeout(
function() {
this.setState({position: 1});
}
.bind(this),
3000
);
否则,您传递的结果是 setState
到 setTimeout
。
Otherwise, you are passing the result of setState
to setTimeout
.
这篇关于ReactJS:setTimeout()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文