是的 它是异步的。我发布这个是因为这对于新的React用户而言并不是立即显而易见的。
如果您需要执行依赖于新状态更改的代码块,请像这样传递回调:
getInitialState: function () { return { isFinalCountdown: false, }}//blablabla//then somewhere you got...this.setState( {isFinalCountdown: true}, function () {//<--- whoa. this solves your all your synchrosity woes! console.log(this.state.isFinalCountdown); //true! });console.log(this.state.isFinalCountdown); //false!
所有这些都在文档中,这只是真正需要重申的内容,以避免新的React用户可能遇到的常见错误。
- 签出:https
- //facebook.github.io/react/docs/component-
api.html#setstate
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)