美文网首页react
react 生命周期

react 生命周期

作者: 暴躁程序员 | 来源:发表于2022-04-13 09:16 被阅读0次

1. 组件初始化触发

执行顺序:constructor > UNSAFE_componentWillMount > render > componentDidMount

  1. 构造函数 constructor
    常用于定义 state 对象、为自定义方法绑定 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,组件正在渲染
    常用于定义 dom 元素和组件,一般不在内部书写业务逻辑
render() {
    console.log("render 周期");
    return (
      <>
        {this.state.id}
        {this.state.name}
      </>
    );
  }
  1. componentDidMount,render 函数渲染后触发
    常用获取 DOM 节点后的后续的操作
componentDidMount() {
    console.log("componentDidMount 周期");
  }

2. 组件更新触发

执行顺序:UNSAFE_componentWillReceiveProps > shouldComponentUpdate > UNSAFE_componentWillUpdate > render > componentDidUpdate

  1. UNSAFE_componentWillReceiveProps,当父组件修改子组件 state 时触发,只是组件本身修改自己的 state 不会触发
UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
    console.log("UNSAFE_componentWillReceiveProps 周期");
  }
  1. shouldComponentUpdate,state 修改后,组件渲染前触发,决定是否使用新数据渲染组件,必须 return
    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. render,组件渲染时触发
render() {
    console.log("render(渲染函数) 周期");
    return (
      <>
        <div
          onClick={() => {
            this.setState({
              id: "10000000000",
              name: "改变state",
            });
          }}
        >
          Child1
        </div>
        <div>
          {this.state.id}
          {this.state.name}
        </div>
      </>
    );
  }
  1. componentDidUpdate,render 渲染后触发
componentDidUpdate(prevProps, prevState, snapshot) {
    console.log("componentDidUpdate 周期");
  }

3. 组件卸载触发

componentWillUnmount 组件卸载时触发

componentWillUnmount() {
    console.log("componentWillUnmount 周期");
  }

相关文章

  • 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/riwosrtx.html