react生命周期:掛載,更新,錯誤處理,卸載
掛載:constructor()、static getDerivedStateFromProps()、render()、componentDidMount()
更新:static getDerivedStateFromProps()、shouldComponentUpdate()、render()、getSnapshotBeforeUpdate()、componentDidUpdate()
錯誤處理:static getDerivedStateFromError()、componentDidCatch()
卸載:componentWillUnmount()

react生命周期函數:
constructor():組件構造函數,在組件掛載之前調用;僅用於初始化內部state以及為事件處理函數綁定實例;
static getDerivedStateFromProps():會在調用 render 方法之前調用,並且在初始掛載及后續更新時都會被調用,此方法適用於state 的值在任何時候都取決於 props;
render():是 class 組件中唯一必須實現的方法;
componentDidMount:會在組件掛載后(插入 DOM 樹中)立即調用;
shouldComponentUpdate():根據該函數的返回值,來確定組件是否重新渲染;
getSnapshotBeforeUpdate():在最近一次渲染輸出(提交到 DOM 節點,真正的UI變化)之前調用;此生命周期方法的任何返回值將作為參數傳遞給 componentDidUpdate();
componentDidUpdate():會在更新(真正UI變化)后會被立即調用,首次渲染不會執行此方法;
componentWillUnmount():會在組件卸載及銷毀之前直接調用;
static getDerivedStateFromError():此生命周期會在后代組件拋出錯誤后被調用。 它將拋出的錯誤作為參數,並返回一個值以更新 state;它會在渲染階段調用,因此不允許出現副作用
componentDidCatch():此生命周期在后代組件拋出錯誤后被調用,會在“提交”階段被調用,因此允許執行副作用。
父子組件生命周期函數執行順序:
進入頁面:parent-constructor -> parent-getDerivedStateFromProps -> parent-render -> child-constructor -> child-getDerivedStateFromProps -> child-render -> child-componentDidMount -> parent-componentDidMount
更新頁面:parent-getDerivedStateFromProps -> parent-shouldComponentUpdate -> parent-render -> child-getDerivedStateFromProps -> child-shouldComponentUpdate -> child-render -> child-getSnapshotBeforeUpdate ->parent-getSnapshotBeforeUpdate -> child-componentDidUpdate -> parent-componentDidUpdate
銷毀頁面:parent-componentWillUnmount -> child-componentWillUnmount
關於react生命周期詳情可以查看連接:https://zh-hans.reactjs.org/docs/react-component.html#the-component-lifecycle
