可以把組件生命周期大致分為三個階段:
外部影響周期
組件周期結束
getDefaultProps: 在組件創建之前,會先調用 getDefaultProps(),這是 全局調用壹次 ,相當於構造方法
getInitialState: 在組件創建並加載後,調用getInitialState(),用來初始化組件的狀態。
componentwillmount: 準備加載組件,調用壹次這個函數調用時機是在組件創建,並初始化了狀態之後,在第壹次繪制render()之前。可以在這裏做壹些業務初始化操作,相當於安卓裏的onMeasure方法
render: 組件渲染函數,會返回壹個Virtual DOM。應該保持render函數的純凈,只渲染組件,不修改狀態。
componentDid Mount: 這個函數調用的時候,其虛擬DOM已經構建完成,妳可以在這個函數開始獲取其中的元素或者子組件了,相當於安卓裏的onLayout方法
componentWillReceiveProps: 接受父組件新的屬性(props),相當於set()方法
shouldComponentUpdate: 當組件接收到新的屬性和狀態改變的話,都會觸發shouldCoomponentUpdate,相當於安卓裏的onInterceptTouchEvent方法
componentwillupdate: 如果組件狀態或者屬性改變,並且上面的 shouldCoomponentUpdate(...)返回為true,相當於安卓裏的onTouchEvent方法
componentDidUpdate: 在render之後,會生成真實的DOM,然後調用componentDidUpdate(prevProps, prevState),傳遞的參數是當前的props和state。