【发布时间】:2019-08-28 06:33:59
【问题描述】:
我不明白 shouldComponentUpdate() 方法如何访问旧状态,据我所知,shouldComponentUpdate() 在道具或状态更改后被触发。
假设您调用 setState() ,之后当前状态将被更改,并将更新为新状态。但是即使在将状态更新到最新版本之后也会触发 shouldComponentUpdate()。那么 shouldComponentUpdate() 中的 this.state 怎么能返回旧状态呢?
shouldComponentUpdate(nextProps, nextState) {
console.log(this.state); // how can this be old state?
}
有人知道吗?非常感谢。
【问题讨论】:
标签: javascript reactjs react-lifecycle-hooks