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对象