我想设置状态,然后在设置它之后调用当前状态的另一个函数。this.setState不工作
我有这样的代码
this.setState({
candidates : diCandi
},()=>{
this.createDataSource(this.state)
console.log("The state is=>",this.state);
});
我想打电话给this.createDataSource(this.state)
与我刚刚更新的状态。 它不工作我不知道为什么。有什么我做错了
Infact当我注销状态。我从来没有看到日志中的代码。
_not working_是不够的信息。请给你的问题更多的背景。如果您收到任何错误或奇怪的行为,请将其添加到您的问题。 – bennygenel
这应该工作,回调的关键是它不会触发,直到状态更新(因为这可能是异步)。请使用Stack Snippets('[<>]'工具栏按钮)使用** runnable ** [mcve]更新您的问题。 Stack Snippets支持React,包括JSX; [这是如何做一个](http://meta.stackoverflow.com/questions/338537/)。 –
澄清什么是“不能工作”。你可以尝试在'createDataSource'内登录,看看状态是否是新的(它应该是)。所以你需要找出什么是不工作的。 – Panther