I have this scenario:
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
}
Now running this I get
A
false
So what am I missing here? Shouldn't getDerivedStateFromProps update the state?
Okay, I realized the error quite quickly now. To use the updated state in
shouldComponentUpdate
you have to use the parametersshouldComponentUpdate(nextProps, nextState)
and notthis.state
.