react 生命周期

一、模拟测试

  1. 定义父组件
import React, { Component } from "react";
import OptimizeChildView from "./OptimizeChildView";

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      a: 1,
      b: 1,
    };
    console.log("父组件 constructor");
  }
  
  // 组件创建
  UNSAFE_componentWillMount() {
    console.log("父组件 UNSAFE_componentWillMount");
  }
  componentDidMount() {
    console.log("父组件 componentDidMount");
  }

  // 组件更新
  UNSAFE_componentWillReceiveProps () {
    console.log("父组件 UNSAFE_componentWillReceiveProps");
  }
  shouldComponentUpdate () {
    console.log("父组件 shouldComponentUpdate");
    return true
  }
  UNSAFE_componentWillUpdate () {
    console.log("父组件 UNSAFE_componentWillUpdate ");
  }
  componentDidUpdate() {
    console.log("父组件 componentDidUpdate");
  }
  
  // 组件卸载
  componentWillUnmount () {
    console.log("父组件 componentWillUnmount");
  }
  render() {
    console.log("父组件 render");
    return (
      <>
        <h1>父组件</h1>
        <button onClick={()=>this.setState({a:this.state.a + 1})}>改变状态</button>
        <button onClick={()=>this.setState({b:this.state.b + 1})}>卸载组件</button>
        { this.state.b === 1 ? <OptimizeChildView /> : '' }
      </>
    );
  }
}
export default Test;
  1. 定义子组件
import React, { Component } from "react";

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {};
    console.log("子组件 constructor");
  }
  // 组件创建
  UNSAFE_componentWillMount() {
    console.log("子组件 UNSAFE_componentWillMount");
  }
  componentDidMount() {
    console.log("子组件 componentDidMount");
  }


  // 组件更新
  UNSAFE_componentWillReceiveProps () {
    console.log("子组件 UNSAFE_componentWillReceiveProps");
  }
  shouldComponentUpdate () {
    console.log("子组件 shouldComponentUpdate");
    return true
  }
  UNSAFE_componentWillUpdate () {
    console.log("子组件 UNSAFE_componentWillUpdate ");
  }
  componentDidUpdate() {
    console.log("子组件 componentDidUpdate");
  }
  
  // 组件卸载
  componentWillUnmount () {
    console.log("子组件 componentWillUnmount");
  }
  render() {
    console.log("子组件 render");
    return (
      <>
        <h1>子组件</h1>
      </>
    );
  }
}
export default Test;

二、生命周期执行顺序

  1. 组件创建阶段
父组件 constructor
父组件 UNSAFE_componentWillMount
父组件 render
子组件 constructor
子组件 UNSAFE_componentWillMount
子组件 render
子组件 componentDidMount
父组件 componentDidMount
  1. 组件更新阶段
父组件 shouldComponentUpdate
父组件 UNSAFE_componentWillUpdate 
父组件 render
子组件 UNSAFE_componentWillReceiveProps
子组件 shouldComponentUpdate
子组件 UNSAFE_componentWillUpdate 
子组件 render
子组件 componentDidUpdate
父组件 componentDidUpdate
  1. 组件卸载阶段
子组件 componentWillUnmount

三、生命周期功能说明

  1. 构造函数 constructor
    常用于定义 state 对象、为自定义方法通过 bind 绑定 this
constructor(props) {
  console.log("constructor 周期");
  super(props);
  this.state = {
    id: "100",
    name: "小明",
  };
}
  1. UNSAFE_componentWillMount,render 函数渲染前触发
    常用于获取后端接口请求数据,接收参数,修改 state
UNSAFE_componentWillMount(nextProps, nextState, nextContext) {
  console.log("UNSAFE_componentWillMount 周期");
}
  1. 渲染函数 render
    常用于定义jsx模板,一般不在内部书写业务逻辑
render() {
  console.log("render 周期");
  return (
    <>
      {this.state.id}
      {this.state.name}
    </>
  );
}
  1. componentDidMount,render 函数渲染后触发
    常用获取 DOM 节点后的后续的操作
componentDidMount() {
  console.log("componentDidMount 周期");
}
  1. UNSAFE_componentWillReceiveProps,当组件从父组件中接收参数,且父组件的state被修改后(此修改不包括第一次组件初始化的时候)会触发此组件中的这个生命周期
UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
  console.log("UNSAFE_componentWillReceiveProps 周期");
}
  1. shouldComponentUpdate,state或者props改变后触发,决定是否执行 render 函数,用于优化不必要的 render 渲染
    return true 时,不阻止组件渲染,继续执行下面的生命周期
    return false 时,阻止组件渲染,不执行下面的生命周期
shouldComponentUpdate(nextProps, nextState, nextContext) {
  console.log("shouldComponentUpdate 周期");
  return true;
  // return false;
}
  1. UNSAFE_componentWillUpdate,render 渲染前触发
UNSAFE_componentWillUpdate(nextProps, nextState, nextContext) {
  console.log("UNSAFE_componentWillUpdate 周期");
}
  1. componentDidUpdate,render 渲染后触发
componentDidUpdate(prevProps, prevState, snapshot) {
  console.log("componentDidUpdate 周期");
}
  1. componentWillUnmount 组件卸载时触发
componentWillUnmount() {
  console.log("componentWillUnmount 周期");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容