我对 React v16.4 生命周期的理解

34,784 阅读8分钟

关于react生命周期的文章,网上一大堆,本人也看了许多,但是觉得大部分人写的都是照搬其它人的没有自己独到的见解,所以决定根据本人的实战经验和个人理解再写一篇React生命周期的文章,由于React目前已更新到16.4版本,所以重点讲解React v16.4变化的生命周期,之前的生命周期函数会一带而过

先总体看下React16的生命周期图

React16废弃的三个生命周期函数

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

注:目前在16版本中componentWillMountcomponentWillReceivePropscomponentWillUpdate并未完全删除这三个生命周期函数,而且新增了UNSAFE_componentWillMountUNSAFE_componentWillReceivePropsUNSAFE_componentWillUpdate三个函数,官方计划在17版本完全删除这三个函数,只保留UNSAVE_前缀的三个函数,目的是为了向下兼容,但是对于开发者而言应该尽量避免使用他们,而是使用新增的生命周期函数替代它们

取而代之的是两个新的生命周期函数

  • static getDerivedStateFromProps
  • getSnapshotBeforeUpdate

我们将React的生命周期分为三个阶段,然后详细讲解每个阶段具体调用了什么函数,这三个阶段是:

  • 挂载阶段
  • 更新阶段
  • 卸载阶段

挂载阶段

挂载阶段,也可以理解为组件的初始化阶段,就是将我们的组件插入到DOM中,只会发生一次

这个阶段的生命周期函数调用如下:

  • constructor
  • getDerivedStateFromProps
  • componentWillMount/UNSAVE_componentWillMount
  • render
  • componentDidMount

constructor

组件构造函数,第一个被执行

如果没有显示定义它,我们会拥有一个默认的构造函数

如果显示定义了构造函数,我们必须在构造函数第一行执行super(props),否则我们无法在构造函数里拿到this对象,这些都属于ES6的知识

在构造函数里面我们一般会做两件事:

  • 初始化state对象
  • 给自定义方法绑定this
constructor(props) {
    super(props)
    
    this.state = {
      select,
      height: 'atuo',
      externalClass,
      externalClassText
    }

    this.handleChange1 = this.handleChange1.bind(this)
    this.handleChange2 = this.handleChange2.bind(this)
}

禁止在构造函数中调用setState,可以直接给state设置初始值

getDerivedStateFromProps

static getDerivedStateFromProps(nextProps, prevState)

一个静态方法,所以不能在这个函数里面使用this,这个函数有两个参数props和state,分别指接收到的新参数和当前的state对象,这个函数会返回一个对象用来更新当前的state对象,如果不需要更新可以返回null

该函数会在挂载时,接收到新的props,调用了setState和forceUpdate时被调用

在React v16.3时只有在挂载时和接收到新的props被调用,据说这是官方的失误,后来修复了

这个方法就是为了取代之前的componentWillMountcomponentWillReceivePropscomponentWillUpdate

当我们接收到新的属性想去修改我们state,可以使用getDerivedStateFromProps

class ExampleComponent extends React.Component {
  state = {
    isScrollingDown: false,
    lastRow: null
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.currentRow !== prevState.lastRow) {
        return {
            isScrollingDown:
            nextProps.currentRow > prevState.lastRow,
            lastRow: nextProps.currentRow
        }
    }
    return null
  }
}

componentWillMount/UNSAFE_componentWillMount

在16版本这两个方法并存,但是在17版本中componentWillMount被删除,只保留UNSAFE_componentWillMount,目的是为了做向下兼容,对于新的应用,用getDerivedStateFromProps代替它们

由于componentWillMount/ UNSAFE_componentWillMount是在render之前调用,所以就算在这个方法中调用setState也不会触发重新渲染(re-render)

render

React中最核心的方法,一个组件中必须要有这个方法

返回的类型有以下几种:

  • 原生的DOM,如div
  • React组件
  • Fragment(片段)
  • Portals(插槽)
  • 字符串和数字,被渲染成text节点
  • Boolean和null,不会渲染任何东西

关于Fragment和Portals是React16新增的,如果大家不清楚可以去阅读官方文档,在这里就不展开了

render函数是纯函数,里面只做一件事,就是返回需要渲染的东西,不应该包含其它的业务逻辑,如数据请求,对于这些业务逻辑请移到componentDidMount和componentDid Update中

componentDidMount

组件装载之后调用,此时我们可以获取到DOM节点并操作,比如对canvas,svg的操作,服务器请求,订阅都可以写在这个里面,但是记得在componentWillUnmount中取消订阅

componentDidMount() {
    const { progressCanvas, progressSVG } = this

    const canvas = progressCanvas.current
    const ctx = canvas.getContext('2d')
    canvas.width = canvas.getBoundingClientRect().width
    canvas.height = canvas.getBoundingClientRect().height

    const svg = progressSVG.current
    const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
    rect.setAttribute('x', 0)
    rect.setAttribute('y', 0)
    rect.setAttribute('width', 0)
    rect.setAttribute('height', svg.getBoundingClientRect().height)
    rect.setAttribute('style', 'fill:red')

    const animate = document.createElementNS('http://www.w3.org/2000/svg', 'animate')
    animate.setAttribute('attributeName', 'width')
    animate.setAttribute('from', 0)
    animate.setAttribute('to', svg.getBoundingClientRect().width)
    animate.setAttribute('begin', '0ms')
    animate.setAttribute('dur', '1684ms')
    animate.setAttribute('repeatCount', 'indefinite')
    animate.setAttribute('calcMode', 'linear')
    rect.appendChild(animate)
    svg.appendChild(rect)
    svg.pauseAnimations()

    this.canvas = canvas
    this.svg = svg
    this.ctx = ctx
 }

在componentDidMount中调用setState会触发一次额外的渲染,多调用了一次render函数,但是用户对此没有感知,因为它是在浏览器刷新屏幕前执行的,但是我们应该在开发中避免它,因为它会带来一定的性能问题,我们应该在constructor中初始化我们的state对象,而不应该在componentDidMount调用state方法

更新阶段

更新阶段,当组件的props改变了,或组件内部调用了setState或者forceUpdate发生,会发生多次

这个阶段的生命周期函数调用如下:

  • componentWillReceiveProps/UNSAFE_componentWillReceiveProps
  • getDerivedStateFromProps
  • shouldComponentUpdate
  • componentWillUpdate/UNSAFE_componentWillUpdate
  • render
  • getSnapshotBeforeUpdate
  • componentDidUpdate

componentWillReceiveProps/UNSAFE_componentWillReceiveProps

componentWillReceiveProps(nextProps, prevState) UNSAFE_componentWillReceiveProps(nextProps, prevState)

在16版本这两个方法并存,但是在17版本中componentWillReceiveProps被删除,UNSAFE_componentWillReceiveProps,目的是为了做向下兼容,对于新的应用,用getDerivedStateFromProps代替它们

注意,当我们父组件重新渲染的时候,也会导致我们的子组件调用componentWillReceiveProps/UNSAFE_componentWillReceiveProps,即使我们的属性和之前的一样,所以需要我们在这个方法里面去进行判断,如果前后属性不一致才去调用setState

在装载阶段这两个函数不会被触发,在组件内部调用了setState和forceUpdate也不会触发这两个函数

getDerivedStateFromProps

这个方法在装载阶段已经讲过了,这里不再赘述,记住在更新阶段,无论我们接收到新的属性,调用了setState还是调用了forceUpdate,这个方法都会被调用

shouldComponentUpdate

shouldComponentUpdate(nextProps, nextState)

有两个参数nextProps和nextState,表示新的属性和变化之后的state,返回一个布尔值,true表示会触发重新渲染,false表示不会触发重新渲染,默认返回true

注意当我们调用forceUpdate并不会触发此方法

因为默认是返回true,也就是只要接收到新的属性和调用了setState都会触发重新的渲染,这会带来一定的性能问题,所以我们需要将this.props与nextProps以及this.state与nextState进行比较来决定是否返回false,来减少重新渲染

但是官方提倡我们使用PureComponent来减少重新渲染的次数而不是手工编写shouldComponentUpdate代码,具体该怎么选择,全凭开发者自己选择

在未来的版本,shouldComponentUpdate返回false,仍然可能导致组件重新的渲染,这是官方自己说的

Currently, if shouldComponentUpdate() returns false, then UNSAFE_componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. In the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.

componentWillUpdate/UNSAFE_componentWillUpdate

componentWillUpdate(nextProps, nextState)
UNSAFE_componentWillUpdate(nextProps, nextState)

在16版本这两个方法并存,但是在17版本中componentWillUpdate被删除,UNSAFE_componentWillUpdate,目的是为了做向下兼容

在这个方法里,你不能调用setState,因为能走到这个方法,说明shouldComponentUpdate返回true,此时下一个state状态已经被确定,马上就要执行render重新渲染了,否则会导致整个生命周期混乱,在这里也不能请求一些网络数据,因为在异步渲染中,可能会导致网络请求多次,引起一些性能问题,

如果你在这个方法里保存了滚动位置,也是不准确的,还是因为异步渲染的问题,如果你非要获取滚动位置的话,请在getSnapshotBeforeUpdate调用

render

更新阶段也会触发,装载阶段已经讲过了,不再赘述

getSnapshotBeforeUpdate

getSnapshotBeforeUpdate(prevProps, prevState)

这个方法在render之后,componentDidUpdate之前调用,有两个参数prevProps和prevState,表示之前的属性和之前的state,这个函数有一个返回值,会作为第三个参数传给componentDidUpdate,如果你不想要返回值,请返回null,不写的话控制台会有警告

还有这个方法一定要和componentDidUpdate一起使用,否则控制台也会有警告

前面说过这个方法时用来代替componentWillUpdate/UNSAVE_componentWillUpdate,下面举个例子说明下:

class ScrollingList extends React.Component {
  constructor(props) {
    super(props);
    this.listRef = React.createRef();
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    // Are we adding new items to the list?
    // Capture the scroll position so we can adjust scroll later.
    if (prevProps.list.length < this.props.list.length) {
      const list = this.listRef.current;
      return list.scrollHeight - list.scrollTop;
    }
    return null;
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    // If we have a snapshot value, we've just added new items.
    // Adjust scroll so these new items don't push the old ones out of view.
    // (snapshot here is the value returned from getSnapshotBeforeUpdate)
    if (snapshot !== null) {
      const list = this.listRef.current;
      list.scrollTop = list.scrollHeight - snapshot;
    }
  }

  render() {
    return (
      <div ref={this.listRef}>{/* ...contents... */}</div>
    );
  }
}

componentDidUpdate

componentDidUpdate(prevProps, prevState, snapshot)

该方法在getSnapshotBeforeUpdate方法之后被调用,有三个参数prevProps,prevState,snapshot,表示之前的props,之前的state,和snapshot。第三个参数是getSnapshotBeforeUpdate返回的

在这个函数里我们可以操作DOM,和发起服务器请求,还可以setState,但是注意一定要用if语句控制,否则会导致无限循环

卸载阶段

卸载阶段,当我们的组件被卸载或者销毁了

这个阶段的生命周期函数只有一个:

  • componentWillUnmount

componentWillUnmount

当我们的组件被卸载或者销毁了就会调用,我们可以在这个函数里去清除一些定时器,取消网络请求,清理无效的DOM元素等垃圾清理工作

注意不要在这个函数里去调用setState,因为组件不会重新渲染了

最后

查看 React v16.4 生命周期

你们的打赏是我写作的动力

微信
支付宝