簡體   English   中英

getDerivedStateFromProps 不更新 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM