constructor()
componentWillMount()
render()
componentDidMount()
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
componentWillUnmount()
constructor()
的參數props
獲取
同步
地設置狀態將不會觸發重渲染setState()
方法來改變狀態值注意:不要在render方法中調用 setState()
方法,不然會遞歸渲染javascript
render()
,render()
又從新改變狀態
setState()
修改狀態的值componentDidMount() {
// 此時,就能夠獲取到組件內部的DOM對象 console.warn('componentDidMount', document.getElementById('btn')) }
props
或者state
改變的時候,都會觸發運行階段的函數
props
前觸發這個方法props
值this.props
獲取到上一次的值this.props
和nextProps
並在該方法中使用this.setState()
處理狀態改變state
不會觸發該方法
componentWillReceiveProps(nextProps) {
console.warn('componentWillReceiveProps', nextProps) }
true
從新渲染,不然不渲染false
,那麼,後續render()
方法不會被調用// - 參數: // - 第一個參數:最新屬性對象 // - 第二個參數:最新狀態對象 shouldComponentUpdate(nextProps, nextState) { console.warn('shouldComponentUpdate', nextProps, nextState) return nextState.count % 2 === 0 }
Mounting
階段的render
是同一個函數做用:在卸載組件的時候,執行清理工做,好比html
componentDidMount
建立的DOM對象