组件被创建插入DOM中的时候,它的生命周期如下:
constructor()
static getDerivedStateFromProps()
render()
componentDidMount()
constructor()构造函数
组件被加载时,最先调用(仅仅一次)
其中的作用是定义状态变量
第一个语句为super(props)
componentWillMount()
组件即将被装载、渲染到页面上(render()被调用之前)调用(仅仅调用一次)
在函数中调用setState中改变状态,react等setState完成后再渲染组件
render()
在componentWillMount()之后,在componentDidMount()之前
生成虚拟的DOM节点
渲染挂载组件
初始化页面、接收新的props(父组件更新)
componentDidMount()
在render()之后被调用,仅仅调用一次
组件真正被装载之后。
componentWillUnmount
组件被卸载时调用
在该函数中执行任何必要的清理,定时器
componentWillUpdate
组件即将更新,不能修改属性和状态
componentWillUpdate()
组件已经更新
shouldComponentUpdate()
组件接收新的属性或者新的状态的时候调用(可以返回false,接受数据后不更新,阻止render调用,之后的函数将不被执行)
componentWillReceiveProps
组件将接收到属性的时候调用,
网友评论