wle*_*300 3 javascript state asynchronous synchronous reactjs
嗯.我正在使用setState并且由于某种原因,它后面的代码无法访问新状态!
是什么赋予了?!
叶氏.这是异步的.我发布这个是因为这对新的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!
Run Code Online (Sandbox Code Playgroud)
所有这些都在文档中,它只是需要重复的东西,以避免新的React用户可能遇到的常见错误.
看看:https://facebook.github.io/react/docs/component-api.html#setstate