Skip to content

Latest commit

 

History

History
29 lines (19 loc) · 1.03 KB

5.lifecycle.md

File metadata and controls

29 lines (19 loc) · 1.03 KB

生命週期

componentWillMount

  • render 前會呼叫,在這邊可以做一次 setState,來更新 state

componentDidMount

  • 第一次 render 完成後會呼叫,在這邊適合來跟 server 要資料來更新 View,setState 會觸發更新

componentWillReceiveProps

componentWillReceiveProps(nextProps)

  • 當父元素傳入的 props 有更新會觸發這個生命週期,如果有 state 根據 props 來跟動,可在這做 setState

shouldComponentUpdate

shouldComponentUpdate(nextProps, nextState)

  • 可以根據 nextProps、nextState 來跟當前 this.props、this.state 來對比,在特殊情況下 return false 可以不更新 view,預設 retur true

componentWillUpdate

componentWillUpdate(nextProps, nextState)

  • 做 update render 前最後一個生命週期,這裡無法使用 setState

componentDidUpdate

  • update 成功後的生命週期

componentWillUnmount

  • component 卸載或是刪除時觸發