React生命周期简介

2018-08-16  本文已影响441人  技术体验师_萦回

首先介绍react16.3之前的生命周期;之后介绍react16.3的生命周期的变化

react16.3之前的生命周期

react的生命周期分为三个阶段:

  1. Mounting 挂载
  2. Updating 更新
  3. Unmounting 卸载

首先列举出所有的生命周期函数,再与这三个阶段对应、解释

  constructor(props) {
    console.log('constructor');
    super(props);
  }

  componentWillMount() {
    console.log('componentWillMount');
  }

  componentDidMount() {
    console.log('componentDidMount');
  }

  componentWillReceiveProps(nextProps, nextState) {
    console.log('componentWillReceiveProps');
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate');
    return true;
  }

  componentWillUpdate(nextProps, nextState) {
    console.log('componentWillUpdate');
  }

  componentDidUpdate(prevProps, prevState) {
    console.log('componentDidUpdate');
  }

  componentWillUnmount(){
    console.log('componentWillUnmount');
  }
Mounting 挂载阶段:
Updating 更新阶段:
Unmounting 卸载阶段:

附上代码,可自行运行查看运行顺序

/**
 * react16.3前生命周期函数
 */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class Father extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 1
    }
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState((prevState, props) => ({
      count: prevState.count + 1
    }))
  }

  render() {
    return (
      <div style={{ border: "1px solid blue" }}>
        <Child count={this.state.count} handleClick={this.handleClick} />
      </div>
    )
  }
}
class Child extends Component {
  constructor(props) {
    console.log('constructor');
    super(props);
  }

  componentWillMount() {
    console.log('componentWillMount');
  }

  componentDidMount() {
    console.log('componentDidMount');
  }

  componentWillReceiveProps(nextProps, nextState) {
    console.log('componentWillReceiveProps');
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate');
    return true;
  }

  componentWillUpdate(nextProps, nextState) {
    console.log('componentWillUpdate');
  }

  componentDidUpdate(prevProps, prevState) {
    console.log('componentDidUpdate');
  }

  componentWillUnmount() {
    console.log('componentWillUnmount');
  }

  render() {
    console.log('render');
    return (
      <div style={{ margin: "15px", border: "1px solid red" }}>
        子元素:
        <br />
        父组件属性count值: {this.props.count}
        <br />
        <span onClick={() => this.props.handleClick()}
          style={{ display: "inline-block", padding: "3px 5px", color: "#ffffff", background: "green", borderRadius: "3px", cursor: "pointer" }}
        >click me</span>
      </div>
    )
  }
}
ReactDOM.render(
  <Father />
  , document.getElementById('root'))

运行结果如下

 constructor
 componentWillMount
 render
 componentDidMount

点击button按钮(click me)后的控制台运行结果如下:

constructor
componentWillMount
render
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate

下图反映了React组件四条更新路径:

react-lifecycle.png

react16.3之后的生命周期

react16.3新增了一些生命周期函数:

getDerivedStateFromProps、getSnapshotBeforeUpdate

同时FB声明在react17将删除:

componentWillMount、componentWillReceiveProps、componentWillUpdate

在16.3的版本中将保留这三个函数,并添加了别名:

UNSAFE_componentWillMount、UNSAFE_componentWillReceiveProps、UNSAFE_componentWillUpdate

在此我们主要介绍

getDerivedStateFromProps、getSnapshotBeforeUpdate

/**
 * react16.3后生命周期
 */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class Father extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 1
    }
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState((prevState, props) => ({
      count: prevState.count + 1
    }))
  }

  render() {
    return (
      <div style={{ border: "1px solid blue" }}>
        <Child count={this.state.count} handleClick={this.handleClick} />
      </div>
    )
  }
}
class Child extends Component {
  constructor(props) {
    console.log('constructor');
    super(props);
    this.state={
      count:10
    }
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    console.log('getDerivedStateFromProps');
    return {
      count: nextProps.count * 2,
    };
  }

  componentDidMount() {
    console.log('componentDidMount');
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate');
    return true;
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('getSnapshotBeforeUpdate');
    return 'aa';
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('componentDidUpdate');
    console.log(snapshot);
  }

  render() {
    console.log('render');
    return (
      <div style={{ margin: "15px", border: "1px solid red" }}>
        子元素:
        <br />
        父组件属性count值: {this.props.count}
        <br />
        <span onClick={() => this.props.handleClick()}
          style={{ display: "inline-block", padding: "3px 5px", color: "#ffffff", background: "green", borderRadius: "3px", cursor: "pointer" }}
        >click me</span>
      </div>
    )
  }
}
ReactDOM.render(
  (
    <Father />
  )
  , document.getElementById('root'))

执行结果如下

constructor
getDerivedStateFromProps
render
componentDidMount

点击后如下:

constructor
 getDerivedStateFromProps
render
componentDidMount
getDerivedStateFromProps
shouldComponentUpdate
render
getSnapshotBeforeUpdate
componentDidUpdate
aa

运行一下代码一看便知

上一篇 下一篇

猜你喜欢

热点阅读