在使用setState.log后,我的状态没有更新。大多数情况下,log显示默认值。为什么then
不一致?
setState
答案 0 :(得分:2)
您知道setState
是异步的,您应该调用第二个函数作为setState的回调
this.setState({ isApproved: true }, function(){
this._setApproval(true)
});
};
_setApproval(){
console.log("Approved TICK " + this.state.isApproved);
this.verifyApprovals();
}
regectApproval = () => {
this.setState({ isApproved: false } , function() {
this._setRejection();
});
};
_setRejection() {
console.log("Approved" + this.state.isApproved);
this.verifyApprovals();
}