捋一捋React的生命周期

前言:React 的版本从 v15 到 v16.3 ,再到v16.4,如今最新的版本是 v16.8了。其中最大的变化多是React Hooks的加入,而最使人困惑的倒是它的生命周期,新旧生命周期函数混杂在一块儿,不免会让许多新来者有不少困惑。因此这一篇咱们来分析一下 React 生命周期的演变及缘由,进一步理解其使用。react

组件生命周期的四个阶段

  1. Initialization (初始化阶段:组件实例的建立)
  2. Mounting (加载阶段:组件插入 dom中)
  3. Updating (更新阶段:属性或状态每改变一次都将会触发一次,组件从新渲染。)
  4. Unmounting (卸载阶段:组件卸载和销毁)

老版生命周期(16.3以前的生命周期)

clipboard.png

Initialization (初始化阶段:涉及4个钩子函数)

这些方法会在组件初始化的时候被调用,只跟实例的建立有关。
若是用createReactClass进行建立,则还有getInitialState这些生命周期函数,但不多使用,咱们这里不说起。算法

static defaultProps{} (getDefaultProps())

定义默认props,会和父组件传下来的props进行合并,且以父组件中的props优先级更高,至关于{...defaultProps, props}segmentfault

static propTypes{} (getInitialState())

定义props的数据类型,能够帮助咱们肯定其有效性,减小许多开发中没必要要的错误。浏览器

constructor()

在加载阶段前调用一次,进行 state 的初始化。性能优化

constructor(props){
    super(props)
}

super(props)用来调用父类的构建方法。网络

Mounting (加载阶段:涉及3个钩子函数)

componentWillMount()

新版中为UNSAFE_componentWillMount()dom

只在组件加载时调用,整个生命周期只调用一次,之后组件更新也不会调用,此时能够修改 state。async

render()

react 中最重要的生命周期函数,建立虚拟 dom,并进行 diff 算法,更新 dom 树也在此进行。因此不该该在此改变组件的状态或者与浏览器进行交互。ide

注意:这个函数不能缺乏,若是不建立虚拟 dom,能够return null函数

componentDidMount()

组件加载完成后当即调用,整个生命周期只调用一次,能够获取到更新后的 DOM,在此处能够进行网络请求等。

Updating (更新阶段:涉及5个钩子函数)

componentWillReceiveProps()

新版中为UNSAFE_componentWillReceiveProps()

在组件加载后,若是有新的props传递过来,会先调用这个函数,能够在这里调用setState()修改state

componentWillReceiveProps(nextProps)

shouldComponentUpdate()

react中一个重要的性能优化点,组件接收到新的props或者state,返回true表示须要更新 dom,返回false阻止更新。

shouldComponentUpdate(nextProps, nextState)

componentWillUpdate()

新版中为UNSAFE_componentWillUpdate()

组件加载时不调用,只有在组件须要更新(即shouldComponentUpdate返回true)时调用。

componentWillUpdate(nextProps, nextState)

注意:不能在这个方法中调用setState()修改state

render()

同上。

componentDidUpdate()

在组件更新完成后当即被调用,能够进行网络请求等。

componentDidUpdate(prevProps, prevState)

Unmounting (卸载阶段:涉及1个钩子函数)

componentWillUnmount()

在组件被卸载和销毁以前调用,能够在这里处理任何须要的清理工做,好比解除定时器,取消已经发起的网络请求,清理在componentDidMount函数中建立的 DOM 元素。

componentWillUnmount()

Error Handling(错误处理:涉及1个钩子函数)

componentDidCatch()

错误边界捕获,在v16.0刚推出的时候新增长的一个生命周期函数,用于捕获在子组件树中任意地方发生的 JavaScript 错误,一个错误边界不能捕获它本身内部的错误。

componentDidCatch(error, info)

组件的基本写法

import React, {Component} from 'React'

export default class OldReactComponent extends Componet{
    static defaultProps={}
    static propTypes={}
    constructor(props){
        super(props)
    }
    state={}
    componentWillMount(){}
    render(){
        return null
    }
    componentDidMount(){}
    componentWillReceivePorps(nextProps){}
    componentShouldUpdate(nextProps,nextState){
        return true
    }
    componentWillUpdate(nextProps,nextState){}
    componentDidUpdate(){}
    componentWillUnmount(){}
}

新版生命周期(16.4以后的生命周期)

clipboard.png

(带UNSAFE_的函数在之后将会被丢弃,官方也不建议使用,这里再也不列出。)

新版本中,新增了三个生命周期函数:

  • static getDerivedStateFromProps()
  • getSnapshotBeforeUpdate
  • static getDerivedStateFromError()

同时deprecate了一组生命周期API,包括:

  • componentWillReceiveProps
  • componentWillMount
  • componentWillUpdate

能够看出除了shouldComponentUpdate以外,render 以前的全部生命周期都被消灭了。缘由是这些生命周期太多时候没有被正确使用,并且在Fiber以后,若是要开启async rendering,在render函数以前的全部函数,都有可能被执行屡次。

Initialization (初始化阶段:涉及4个钩子函数)

同上。

Mounting (加载阶段:涉及3个钩子函数)

static getDerivedStateFromProps()

由于是静态方法,因此没法访问到组件实例。每次组件调用render前都会被调用,获取新的propsstate(v16.3只会为props的而调用,v16.4修正为propsstate)以后,返回一个对象做为新的state,若是返回null则表示不须要更新;配合componentDidUpdate,能够覆盖componentWillReceiveProps的全部用法。而且它应该是个纯函数,没有反作用(side effect)。

static getDerivedStateFromProps(props,state)

render()

同上。

componentDidMount()

同上。

Updating (更新阶段:涉及5个钩子函数)

static getDerivedStateFromProps()

同上。

shouldComponentUpdate()

同上。

render()

同上。

getSnapshotBeforeUpdate()

触发时间: update发生的时候,在render以后,在组件dom渲染以前;返回一个值,做为componentDidUpdate的第三个参数;配合componentDidUpdate, 能够覆盖componentWillUpdate的全部用法,经常使用于 scroll 位置的定位。

getSnapshotBeforeUpdate(prevProps, prevState)

componentDidUpdate()

同上。

Unmounting (卸载阶段:涉及1个钩子函数)

componentWillUnmount()

同上。

Error Handling(错误处理:涉及2个钩子函数)

componentDidCatch()

同上。

static getDerivedStateFromError()

用于在“render”阶段(非render函数)的错误捕获,应该是个纯函数,没有反作用(side effect)。

static getDerivedStateFromError(error)

组件的基本写法

import React, {Component} from 'React'

export default class OldReactComponent extends Componet{
    static defaultProps={}
    static propTypes={}
    constructor(props){
        super(props)
    }
    state={}
    static getDerivedStateFromProps(props,state){}
    render(){
        return null
    }
    componentDidMount(){}
    componentShouldUpdate(nextProps,nextState){
        return true
    }
    getSnapshotBeforeUpdate(prevProps, prevState){}
    componentDidUpdate(){}
    componentWillUnmount(){}
}

总结

  1. React新的生命周期新增static getDerivedStateFromPropsgetSnapshotBeforeUpdate并弃用componentWillMountcomponentWillReceivePropscomponetWillUpdate(这三个函数将在 React 17中删除)。
  2. 新增static getDerivedStateFromError捕获“render”阶段的错误。

参考

帮你理清React的生命周期

React-新的生命周期(React16版本)

React v16.3以后的组件生命周期函数

清晰大图请点击这里

相关文章
相关标签/搜索