React組件生命週期總結

react組件的生命週期大體分爲三種狀態react

  1. mounting  開始插入真實DOM 初始化render
  2. updating   props 或者 state 更新以後組件的更新狀態 也就是render
  3. unmounting 組件渲染結束,移處真實DOM

其中除了unmounting每一個狀態又有對應的will 和 did 方法共有5個函數。函數

  1. componentWillMount
  2. componentDidMount
  3. componentWillUpdate
  4. componentDidUpdate
  5. componentWillUnmount

除此以外還有兩個特殊的方法shouldComponentUpdate(state或者props改變時會觸發的函數)以及componentWillReceiveProps(props改變會觸發的函數)spa

它們的執行順序分別是component

初始化(init render):生命週期

getDefaultProps => getInitalStates => componentWillMount => render => componentDidMountget

props變化it

componentWillReceiveProps => shouldComponentUpdate => componentWillUpdate => render =>componentDidUpdate渲染

state變化date

shouldComponentUpdate => componentWillUpdate => render => componentDidUpdate方法

這裏能夠看出,Props 比 State 的改變會有多出一個componentWillReceiveProps的回調方法

相關文章
相關標籤/搜索