[英]In React 16.7, after setState call, getDerivedStateFromProps returning null is also updating state
[英]getDerivedStateFromProps not updating state?
我有這種情況:
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 不應該更新 state 嗎?
好的,我現在很快就意識到了錯誤。 要在 shouldComponentUpdate 中使用更新的shouldComponentUpdate
,您必須使用參數shouldComponentUpdate(nextProps, nextState)
而不是this.state
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.