我有这样的场景:
static getDerivedStateFromProps(nextProps, prevState) {
if(...) {
console.log("A")
return {
a: true
}
}
console.log("B")
return {
a: false
}
}
shouldComponentUpdate() {
const { a } = this.state
console.log(a)
return a
}现在运行这个,我得到了
A
false那么我在这里错过了什么呢?getDerivedStateFromProps不应该更新状态吗?
发布于 2021-05-08 16:50:42
好了,我现在很快就意识到了这个错误。要在shouldComponentUpdate中使用更新后的状态,必须使用参数shouldComponentUpdate(nextProps, nextState)而不是this.state。
https://stackoverflow.com/questions/67445664
复制相似问题