【发布时间】:2019-06-11 17:56:22
【问题描述】:
我正在尝试更新我的反应组件中的状态。我调用函数:
async removeActivePaymentMethod() {
console.log('activePaymentMethod',this.state.activePaymentMethod); //value is 20289
const order = this.state.order;
order.activePaymentMethod = null;
await this.setState({ activePaymentMethod: null, order },function () {
console.log('activePaymentMethod',this.state.activePaymentMethod);//value is 20289
});
}
如您所见,我的 console.log 调用在 setState 的回调中。为什么这个值没有更新为 null?
【问题讨论】:
-
离奇。同样有点话题,
order.activePaymentMethod = null背后的原因是什么,看起来像是一个状态突变。 -
以及为什么在传递回调以设置状态时使用 await?
-
setState 不返回承诺,因此等待它没有意义。但这确实很奇怪。
-
您确定没有其他状态更新与此一起批处理并覆盖 null?