官方推荐 Cheat sheet

v16.4.2
组件实例被创建和插入到 DOM 中
constructor()static getDerivedStateFromProps()render()componentDidMount()
componentWillMount()被标记为 legacy, 不推荐再使用
props 与 state 的改变都会引起 update
static getDerivedStateFromProps()shouldComponentUpdate(nextProps, nextState)render()getSnapshotBeforeUpdate()componentDidUpdate()
componentWillUpdate(),componentWillReceiveProps()被标记为 legacy, 不推荐再使用
组件实例被移除出 DOM
componentWillUnmount()异常处理方法,能捕抓到的异常范围有渲染过程,生命周期方法,子组件的 constructor 方法
componentDidCatch(error, info)shouldComponentUpdate(nextProps, nextState)componentWillMount()componentWillUpdate()componentWillReceiveProps()render()| Arrays | Fragments |
| String | Number 视作字符节点 |
| Boolean | null |
当
shouldComponentUpdate()返回false时,render()不会执行
constructor(props)super(props)statestate 值直接复制到 this.statesetState()componentDidMount()componentDidUpdate(prevProps, prevState, snapshot)setState() 方法,但需要有条件比较之后再调用,否则陷入死循环snapshot 参数只有在 实现了 getSnapshotBeforeUpdate() 方法后才会有值当
shouldComponentUpdate()返回false后,componentDidUpdate不会被调用
componentWillUnmount()shouldComponentUpdate(nextProps, nextState)forceUpdate() 调用时不会被调用PureComponent 方法false 并不会阻止子组件的渲染行为JSON.stringify() 来比较前后的状态static getDerivedStateFromProps(props, state)null 表示不需要更新状态关于是否应该使用此方法的指导 You Probably Don’t Need Derived State
getSnapshotBeforeUpdate(prevProps, prevState)setState 有一定的延迟,在这里可以获取一些信息