美文网首页
React生命周期

React生命周期

作者: 三粒黑子球 | 来源:发表于2021-05-14 16:17 被阅读0次

React16.3.0之前生命周期

创建期:

constructor(props)
componentWillMount()
render()
componentDidMount()

运行时:

props发生变化时
componentWillReceiveProps(nextProps)
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate(nextProps, nextState)
render()
componentDidUpdate(prevProps, prevState)
state发生变化时
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate(nextProps, nextState)
render()
componentDidUpdate(prevProps, prevState)

卸载时

componentWillUnmount()

React16.3.0之后的生命周期

创建期:

constructor(props)
getDerivedStateFromProps(props, state) //静态方法 static
render()
componentDidMount()
//或者如下生命周期:

constructor(props)
componentWillMount() / UNSAFE_componentWillMount() //前者会有警告⚠️
render()
componentDidMount()

getDerivedStateFromProps/getSnapshotBeforeUpdatecomponentWillMount/componentWillReceiveProps/componentWillUpdate如果同时存在,React会有警告信息,并且只执行 getDerivedStateFromProps/getSnapshotBeforeUpdate【React@16.3.0(以后)】

运行时:

props发生变化时
getDerivedStateFromProps(props, state) //静态方法 static
shouldComponentUpdate(nextProps, nextState, nextContext)
render()
getSnapshotBeforeUpdate(prevProps, prevState)
componentDidUpdate(prevProps, prevState, snapshot)

// 或者如下生命周期:

componentWillReceiveProps()/UNSAFE_componentWillReceiveProps()//前者会有警告⚠️
shouldComponentUpdate(nextProps, nextState)
componentWillUpdate(nextProps, nextState)
render()
componentDidUpdate(prevProps, prevState, snapshot)
state发生变化时
getDerivedStateFromProps(props, status) //静态方法 static
shouldComponentUpdate(nextProps, nextState, nextContext)
render()
getSnapshotBeforeUpdate(prevProps, prevState)
componentDidUpdate(prevProps, prevState, snapshot)
或者如下生命周期:

shouldComponentUpdate(nextProps, nextState, nextContext)
componentWillUpdate()/UNSAFE_componentWillUpdate()//前者会有警告⚠️
render()
componentDidUpdate(prevProps, prevState, snapshot)

销毁时

componentWillUnmount()
图示生命周期

static getDerivedStateFromProps(props, state)

当组件的state和props发生改变的时候会在 render() 前会被执行

该方法有两个参数props和state;

class SomeView extends Component {
    state = {
        count: 20
    }
    static getDerivedStateFromProps(props, state) {
        return {
            count: 50
        }
    }
    render() {
        return (
            <div>{this.state.age}</div>
        )
    }
}

这个方法允许组件基于 props 的变更来更新其内部状态,以这种方式获得的组件状态被称为派生状态。应该谨慎使用派生状态,可能会引入潜在的错误

getSnapshotBeforeUpdate(prevProps, prevState)

在render()的输出被渲染到DOM之前被调用。使组件能够在它们被更改之前捕获当前值(如滚动位置)。这个生命周期返回的任何值都将作为第三个参数传递给componentDidUpdate()

父组件子组件初始化示意图(网图) 父组件发生改变示意图(网图) 卸载组件示意图(网图)

相关文章

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • 《深入React技术栈》学习笔记Ⅲ

    以下的生命周期都是在 React 15 的生命周期, React 16 的生命周期 API 已经发生变化。Reac...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

网友评论

      本文标题:React生命周期

      本文链接:https://www.haomeiwen.com/subject/rfvqjltx.html