美文网首页
React的生命周期

React的生命周期

作者: 明明你也一样 | 来源:发表于2020-05-19 16:13 被阅读0次

DOM 生命周期

在谈React的生命周期之前,先来看看原始写法的生命周期是什么样的

let app = document.getElementById("app");

// create div
let div = document.createElement("div");

let state = 0;
div.innerHTML = `
  <p>${state}</p>
  <button>+</button>
  <button>die</button>
`;

// mount div
app.appendChild(div);

div.style.border = "1px solid red";

div.querySelector("button").onclick = function() {
  state += 1;
  // update div
  div.querySelector("p").innerText = state;
};

div.querySelectorAll("button")[1].onclick = function() {
  // 把所有引用都清除
  div.querySelector("button").onclick = null;
  div.querySelectorAll("button")[1].onclick = null;
  div.remove();
  div = null; 
  // destroy div
};

上面这段代码描述了一个div的生命周期即创建销毁过程,如果搞懂了这个那React的生命周期也同理。

React 生命周期图解

React生命周期图解

下面可以尝试着用 React 的生命周期来写一遍。但是需要注意的是:React 在 16.3版本以后,将一些生命周期方法列为了不安全的生命周期。至于为什么这些生命周期方法是不安全的,React 认为这些生命周期方法经常被误解和巧妙地滥用,并且预计这种滥用可能会在异步渲染方面带来问题。在 17.0 版本将删除componentWillMountcomponentWillReceivePropscomponentWillUpdate。(从这个版本开始开始,只有新的“UNSAFE_”生命周期名称起作用。)

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

React 生命周期

import React from "react";
import "./styles.css";

export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0
    };
    console.log("create");
  }
  onClick() {
    this.setState({
      count: this.state.count + 1
    });
  }
  UNSAFE_componentWillMount() {
    console.log("will mount");
  }
  componentDidMount() {
    console.log("did mount");
  }
  UNSAFE_componentWillUpdate() {
    console.log("will update");
  }
  componentDidUpdate() {
    console.log("did update");
  }
  render() {
    console.log("mount or update");
    return (
      <div className="App">
        {this.state.count}
        <button onClick={() => this.onClick()}>+</button>
      </div>
    );
  }
}

/*console.log依次打印出
create
will mount
mount or update
did mount
will update
mount or update
did update
*/

总结一下,constructor构造函数是create的作用,组件挂载到DOM上之前会调用UNSAFE_componentWillMount钩子,组件挂载调用render函数,挂载完成调用componentDidMount钩子,如果渲染的数据发生更新,也就是操作this.setState()的时候,在更新之前会调用UNSAFE_componentWillUpdate钩子,组件更新时再调用一次render函数,更新完成调用componentDidUpdate钩子。

相关文章

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • 《深入React技术栈》学习笔记Ⅲ

    以下的生命周期都是在 React 15 的生命周期, React 16 的生命周期 API 已经发生变化。Reac...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

网友评论

      本文标题:React的生命周期

      本文链接:https://www.haomeiwen.com/subject/agpbohtx.html