react16 4新特性(生命週期變化)

2021-09-25 18:11:57 字數 1075 閱讀 2716

按照官方文件

該方法只要父元件呼叫了render方法就會呼叫(會判斷oldprops !== newprops),換句話來說,對於父元件沒有render,子元件的該生命週期就不會呼叫。

遺留方法

*unsafe_componentwillmount()

取消unsafe_componentwillmount()的原因:

訂閱事件造成記憶體洩漏

getsnapshotbeforeupdate(props, state)不傳遞preprops原因:

遺留方法

*unsafe_componentwillupdate(nextprops, nextstate)

*unsafe_componentwillreceiveprops(preprops, prestate)

取消unsafe_componentwillreceiveprops(preprops, prestate) ,unsafe_componentwillupdate(nextprops, nextstate)原因:

unsafe_componentwillreceiveprops(preprops, prestate),static getderivedstatefromprops(props, state)

derive state生命週期中沒有條件的直接接受父元件的props危害如下:

解決辦法:

參考

you probably don't need derived state

React16 4以上生命週期 新

當元件例項被建立並插入 dom 中時,其生命週期呼叫順序如下 constructor static getderivedstatefromprops render componentdidmount componentwillmount 即將在v17.0版本廢棄,慎用!getinitialstate...

react新的生命週期

class a extends react.component 用於替換 componentwillreceiveprops 該函式會在初始化和 update 時被呼叫 因為該函式是靜態函式,所以取不到 this 如果需要對比 prevprops 需要單獨在 state 中維護 static get...

React 生命週期 生命週期方法

生命週期 掛載 更新 解除安裝 元件被建立 執行初始化 並被掛載到dom中,完成元件的第一次渲染 constructor props getderivedstatefromprops props,state render componentdidmount 元件被建立時會首先呼叫元件的構造方法,接受...