生命周期問題
父組件掛載三次子組件:生命周期函數執行流程真實的是這樣的(對比標箭頭的地方)
? ? super constructor
? ? super componentWillMount
? ? super render
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentDidMount
? ? ? ? ->child componentDidMount
? ? ? ? ->child componentDidMount
? ? super componentDidMount
為什么不是這樣的?
? ? super constructor
? ? super componentWillMount
? ? super render
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentDidMount
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentWillMount
? ? ? ? ->child render
? ? ? ? ->child componentDidMount
? ? super componentDidMount
2018-12-14
componentWillMount?在渲染前調用,在客戶端也在服務端。
componentDidMount?: 在第一次渲染后調用,只在客戶端。之后組件已經生成了對應的DOM結構,可以通過this.getDOMNode()來進行訪問。 如果你想和其他JavaScript框架一起使用,可以在這個方法中調用setTimeout, setInterval或者發送AJAX請求等操作(防止異步操作阻塞UI)。
componentWillReceiveProps?在組件接收到一個新的 prop (更新后)時被調用。這個方法在初始化render時不會被調用。
shouldComponentUpdate?返回一個布爾值。在組件接收到新的props或者state時被調用。在初始化時或者使用forceUpdate時不被調用。?
可以在你確認不需要更新組件時使用。
componentWillUpdate在組件接收到新的props或者state但還沒有render時被調用。在初始化時不會被調用。
componentDidUpdate?在組件完成更新后立即調用。在初始化時不會被調用。
componentWillUnmount在組件從 DOM 中移除之前立刻被調用。