React Native setState不一致

时间:2018-07-16 05:56:10

标签: reactjs react-native

在使用setState.log后,我的状态没有更新。大多数情况下,log显示默认值。为什么then不一致?

setState

1 个答案:

答案 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();
}