挂载
当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下:
**constructor()**[static getDerivedStateFromProps()](https://zh-hans.reactjs.org/docs/react-component.html#static-getderivedstatefromprops)/**UNSAFE_componentWillMount()**与getSnapshotBeforeUpdate、getDerivedStateFromProps不能共存;**render()****componentDidMount()**
更新
当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下:
[static getDerivedStateFromProps()](https://zh-hans.reactjs.org/docs/react-component.html#static-getderivedstatefromprops)[shouldComponentUpdate()](https://zh-hans.reactjs.org/docs/react-component.html#shouldcomponentupdate)**UNSAFE_componentWillUpdate()**与getSnapshotBeforeUpdate、getDerivedStateFromProps不能共存;**render()**[getSnapshotBeforeUpdate()](https://zh-hans.reactjs.org/docs/react-component.html#getsnapshotbeforeupdate)**componentDidUpdate()**
卸载
当组件从 DOM 中移除时会调用如下方法:
<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>React 实例</title><script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script><script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script><script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script></head><body><script type="text/babel">class Hello extends React.Component {constructor(props) {console.log('constructor')super(props)this.state = { color: 'red' };}shouldComponentUpdate(nextProps, nextState) {console.log('shouldComponentUpdate', nextProps, nextState)return true}/*** getSnapshotBeforeUpdate() 在最近一次渲染输出(提交到 DOM 节点)之前调用* 可以用在没有ui需要更新时候不更新ui*/getSnapshotBeforeUpdate(prevProps, prevState) {console.log('getSnapshotBeforeUpdate', prevProps, prevState)return null}/*** getDerivedStateFromProps 会在调用 render 方法之前调用,* 并且在初始挂载及后续更新时都会被调用。它应返回一个对象来更新 state,如果返回 null 则不更新任何内容。*/static getDerivedStateFromProps(nextProps, prevState) {console.log('getDerivedStateFromProps', nextProps, prevState)}// 与getSnapshotBeforeUpdate、getDerivedStateFromProps不能共存UNSAFE_componentWillMount() {console.log('UNSAFE_componentWillMount')}// 与getSnapshotBeforeUpdate、getDerivedStateFromProps不能共存UNSAFE_componentWillUpdate(nextProps, nextState) {console.log('UNSAFE_componentWillUpdate', nextProps, nextState)}handleBtnClick() {this.setState({color: this.state.color === 'red' ? 'green' : 'red'})}render() {console.log('render')return (<div style={{ color: this.state.color }}>{this.props.children}<button onClick={() => this.handleBtnClick()}>btn</button></div>);}componentDidMount() {console.log('componentDidMount')}componentWillUnmount() {console.log('componentWillUnmount')}}ReactDOM.render(<div><Hello name="world"><h1>helloworld</h1></Hello></div>,document.body);</script></body></html>
挂载执行了

变更状态执行了

参考
文档介绍生命周期
https://zh-hans.reactjs.org/docs/react-component.html#static-getderivedstatefromprops
