概念
生命周期是指在某一个时刻,组件会自动调用执行的函数。
生命周期流程图
具体生命周期过程
1.Initialization
constructor() //组件初始化时自动执行
2.Mounting
componentWillMount()//在组件即将被挂载到页面的时刻自动执行。先被执行
render() //渲染
componentDidMount() //挂载之后会被执行
3.Updating
3.1.props变化
componentWillReceiveProps() //当一个组件要从父组件接收参数,只要当父组件的render函数被重新执行了,子组件的这个生命周期函数就会被执行。
附:如果这个组件第一次存在于这个父组件中,它不会被执行。如果这个组件组件已经存在于一个组件中,那么才会被执行。
shouldComponentUpdate() //组件被更新前会自动执行 return一个boolean值 告诉组件是否需要更新。如果return false 下面的流程都不会进行更新了(state更新时一样)
componentWillUpdate() //组件被更新之前会自动执行,但是在shouldComponentUpdate之后,如果shouldComponentUpdate return false就不会执行了。
render()
componentDidUpdate() //组件被更新之后会自动执行
3.2.state变化
除了没有componentWillReceiveProps()生命周期函数外,其他过程和props变化一致。
4.Unmounting
componentWillUnmount() //当这个组件即将从页面中被剔除的时刻,会被执行。