按照react官方的说法,带有很多组件的应用在销毁时,释放该组件所占用的资源是非常重要的,生命周期函数应运而生。生命周期函数有相应的执行顺序
init and mount
1. constructor(props)
2. componentWillMount()
3. componentDidMount()
undate of props
1. componentWillReceiveProps(newProps)
与state变化时唯一的不同之处
2. shouldComponentUpdate(newProps,newState)
返回bool类型,当为true时,react组件才会更新,继续向下执行update相关的生命周期函数,父子组件套用时,若不需要更新子组件则可以用此函数返回false
3. componentWillUpdate(nextProps,nextState)
4. render()
5. componentDidUpdate(prevProps, prevState)
update of state
1. shouldComponentUpdate(newProps,newState)
返回bool类型,当为true时,react组件才会更新,继续向下执行update相关的生命周期函数
2. componentWillUpdate(nextProps,nextState)
3. render()
4. componentDidUpdate(prevProps, prevState)
unmount
componetWillUnmount
组件将要从页面中剔除的时候自动执行