close
元件生命週期
主要的三個生命週期為:
- Mounting: 準備要掛載實際 dom 節點時
- Updating: state 異動要重新 render 時
- Unmounting: 即將卸載時
Mounting
getInitialState(): object
於掛載前觸發,若有元件有特定 state 應在此方法中定義與回傳
componentWillMount()
於掛載前觸發
componentDidMount()
於掛載後觸發
Updating
componentWillReceiveProps(object nextProps)
於掛載後收到新的 props 時觸發,這方法應該用來比較 this.props 和 nextProps 後再用 this.setState() 設定新的狀態
shouldComponentUpdate(object nextProps, object nextState): boolean
此方法用來定義是否觸發 render 行為
componentWillUpdate(object nextProps, object nextState)
於收到更新且實際執行 render 前觸發
componentDidUpdate(object prevProps, object prevState)
於收到更新且實際執行 render 後觸發
Unmounting
componentWillUnmount()
於移除 dom 節點前觸發
觸發順序:
參考來源:https://cswleocsw.gitbooks.io/react-guide-learing/content/chapter8.html
文章標籤
全站熱搜
留言列表