React【從無到有從有到無】【參考】componentWillReceiveProps

參考:https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/update/component_will_receive_props.html

 

React 運行流程圖,如下

 

componentWillReceiveProps在初始化render的時候不會執行,它會在Component接受到新的狀態(Props)時被觸發,一般用於父組件狀態更新時子組件的重新渲染。

 

// 這種方式十分適合父子組件的互動,通常是父組件需要通過某些狀態控制子組件渲染亦或銷燬

componentWillReceiveProps(nextProps) {
    //componentWillReceiveProps方法中第一個參數代表即將傳入的新的Props
    if (this.props.sharecard_show !== nextProps.sharecard_show){
        //在這裏我們仍可以通過this.props來獲取舊的外部狀態
        //通過新舊狀態的對比,來決定是否進行其他方法
        if (nextProps.sharecard_show){
            this.handleGetCard();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章