React生命周期流程
一、组件初始化
- constructor() // 初始化state
- UNSAFE_componentWillMount()
- render()
- componentDidMount()
如果存在getDerivedStateFromProps和getSnapshotBeforeUpdate,UNSAFE_componentWillMount()、UNSAFE_componentWillReceiveProps、UNSAFE_componentWillUpdate(nextProps, nextState)将不会执行
- constructor()
- static getDerivedStateFromProps(nextProps, prevState)
- render()
- componentDidMount()
二、state发生改变时
- shouldComponentUpdate(nextProps, nextState)
- UNSAFE_componentWillUpdate(nextProps, nextState)
- render()
- componentDidUpdate(prevProps, prevState)
如果存在getDerivedStateFromProps和getSnapshotBeforeUpdate,UNSAFE_componentWillMount()、UNSAFE_componentWillReceiveProps、UNSAFE_componentWillUpdate(nextProps, nextState)将不会执行
- static getDerivedStateFromProps(nextProps, prevState)
- shouldComponentUpdate(nextProps, nextState)
- render()
- getSnapshotBeforeUpdate(prevProps, prevState)
- componentDidUpdate(prevProps, prevState, snapshot)
三、props发生改变时
- UNSAFE_componentWillReceiveProps(nextProps)
- shouldComponentUpdate(nextProps, nextState)
- UNSAFE_componentWillUpdate(nextProps, nextState)
- render()
- componentDidUpdate(prevProps, prevState)
如果存在getDerivedStateFromProps和getSnapshotBeforeUpdate,UNSAFE_componentWillMount()、UNSAFE_componentWillReceiveProps、UNSAFE_componentWillUpdate(nextProps, nextState)将不会执行
- static getDerivedStateFromProps(nextProps, prevState)
- shouldComponentUpdate(nextProps, nextState)
- render()
- getSnapshotBeforeUpdate(prevProps, prevState)
- componentDidUpdate(prevProps, prevState, snapshot)
组件卸载时
- componentWillUnmount()