lznbuild / my-blog

自己的博客
9 stars 1 forks source link

React生命周期 #22

Open lznbuild opened 4 years ago

lznbuild commented 4 years ago

React 16之前的

初始阶段

getDefaultProps(首次挂载执行,重新挂载不执行)

constructor

getInitialState

当使用class组件构建React组件时,static defaultProps={}就是调用了内部的getDefaultProps方法,constructor中的this.state={}就是调用内部的getInitialState方法。

组件挂载阶段

componentWillMount

render

componentDidMount

组件更新过程

props更新

componentWillReceiveProps(nextProps) // 调用setState不会二次渲染

shouldComponentUpdate(nextProps, nextState) // 返回false,不再执行以下方法

componentWillUpdate(nextProps, nextState) // no setState

render

componentDidUpdate(prevProps, prevState)

state更新

shouldComponentUpdate

componentWillUpdate

render

componentDidUpdate

组件卸载阶段

componentWillUnmount //一般用于执行一些清理方法,如事件回收或是清除定时器

React 16之后的

初始阶段

constructor

getDefaulProps

getInitialState

挂载阶段

getDerivedStateFromProps

render

componentDidMount

更新阶段

(props,state更新) getDerivedStateFromProps

shouldComponentUpdate

render

getSnapsHotBeforeUpdate

componentDidUpdate

错误处理

componentDidCatch