# 一.React中的生命周期
1.componentDidMount
組件已經加載到DOM中會執行這個函數,在這個函數中可以初始化一些將要執行的函數,在React生命周期中只會執行一次。在開發中,在該函數中執行的setState,在隨后通過this.state并不能夠馬上拿到,可以通過定時來獲取。
* * * * *
2.componentWillMount
在組件將要掛載到DOM中執行,這個函數我基本上很少用到。初始化工作我基本上在constructor和componentDidMount中去完成。
* * * * *
3.componentWillUnmount
組件從DOM中移除會執行這個函數,在此可以清理無用的DOM和事件。
* * * * *
4.componentWillUpdate
組件將要更新執行。可以在這個函數中清理在componentDidUpdate綁定的事件(這個方式很有用)。
* * * * *
5.componentDidUpdate
組件已經更新執行這個操作。可以在這個函數中初始化需要state中的數據源作為參數的函數。為了防止初始化多次,可以在componentWillUpdate中清理。
文/力譜宿云(簡書作者)
原文鏈接:http://www.jianshu.com/p/9c26259a817a#
著作權歸作者所有,轉載請聯系作者獲得授權,并標注“簡書作者”。
* * * * *
# 二,在 react 組件內部,方法的順序如下:
1. 生命周期方法(按照時間先后順序依次為: getDefaultProps, getInitialState, componentWillMount,componentDidMount, componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate,componentDidUpdate, componentWillUnmount )
2. 其他的方法
3. render 方法