React SetState未设置状态

时间:2016-10-14 00:17:20

标签: reactjs

我有一个像这样的组件状态:

this.state = {
  mode: 'in',
  current: {
    messageId: '',
  },
};

然后我像这样setState:

const newState = update(this.state, { mode: { $set: 'out' },
                                      current: { messageId: { $set: '123' } } });
this.setState(newState);

我一直这样做,因为我开始学习React并且它起作用了,除非这次它因任何原因无法工作..

使用console.log(newState)表明它们都已正确更新:mode: 'out''current.messageId': '123'

但是,当setState仅设置'current.messageId': '123'mode仍然等于in时。

mode州将页面布局从<InLayout />更改为<OutLayout />。我之前已经多次使用过这种方式,它起作用并且仍在使用......

太困惑了。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用setState后,您无法立即获得变异状态。请检查以下内容。

enter image description here

相关问题