简体   繁体   English

React 组件生命周期

[英]React Component Lifecycle

To my knowledge, when some events happen, react creates a virtual-DOM from scratch and compares it to the old virtual-DOM.据我所知,当某些事件发生时,react 会从头开始创建一个虚拟 DOM,并将其与旧的虚拟 DOM 进行比较。 If that is the case, when the render method is called, react should create components and return them to compare.如果是这种情况,当调用 render 方法时,react 应该创建组件并返回它们进行比较。

import React, { Component, Fragment } from 'react';
import ReactDOM from 'react-dom';
import './index.css';

class Demo extends Component {
  constructor(props) {
    console.log("constructor called");

    super(props);
    this.state = { dummy: 1, };
  }

  render = () => <button onClick={this.props.onChange}> button </button>;
}

class App extends Component {
  state = { num: 0, };

  onChange = () => {
    this.setState({ num: this.state.num+1 }); // mutate state to trigger a render
  }

  render() {
    console.log("rendered");
    return (
      <Fragment>
        <Demo onChange={this.onChange} />
      </Fragment>
    );
  }
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

A quick console log reveals that the constructor is called the first time to mount the Demo component onto the page.快速控制台日志显示构造函数是第一次被调用以将Demo组件安装到页面上。 However, subsequent renders do not create new Demo objects that are compared with the old virtual-DOM (since the constructor is not called).但是,后续渲染不会创建与旧虚拟 DOM 进行比较的新Demo对象(因为未调用构造函数)。

At first, my theory was that when the constructor of Demo is called, it then calls the super constructor to check if a similar object with the same props already exists.一开始,我的理论是,当调用Demo的构造函数时,它会调用超级构造函数来检查是否已经存在具有相同props的类似object。 But that was not the case as proven by moving console.log("constructor called");但事实并非如此,正如移动console.log("constructor called");所证明的那样。 before calling the parent constructor.在调用父构造函数之前。

So my question is how does react know not to create another object?所以我的问题是如何反应知道不创建另一个 object?

The key here is that Demo is not unmounted.这里的关键是Demo没有卸载。 When you first render the app, it renders and mounts the Demo component and passes the onChange prop.当您第一次渲染应用程序时,它会渲染并安装Demo组件并传递onChange属性。 But, when the callback is invoked from Demo it sets the state on App.但是,当从 Demo 调用回调时,它会在 App 上设置 state。 Calling setState in App doesn't unmount the Demo component, so there's no need to mount it again. App 中调用setState并不会卸载 Demo 组件,因此无需再次挂载。 When the component is mounted initially is when the constructor runs.最初安装组件的时间是构造函数运行的时间。 If you had a toggle on the App component that would only show the component within render if a certain condition is true, that would trigger the component to unmount.如果您在 App 组件上有一个切换,它只会在某个条件为真时在渲染中显示该组件,这将触发该组件卸载。

Check out this codesandbox and play around a little: https://codesandbox.io/s/lifecycle-methods-vivn6?file=/src/Lifecycle.js .看看这个代码框并玩一下: https://codesandbox.io/s/lifecycle-methods-vivn6?file=/src/Lifecycle.js

Also, this is a cool diagram to get a sense of what's happening: https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/此外,这是一个很酷的图表,可以让您了解正在发生的事情: https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/ 在此处输入图像描述

The main key is that the constructor runs when the component mounts.主要的关键是当组件挂载时构造函数运行。 It will only run again if the component is unMounted from the DOM and then remounted later.只有当组件从 DOM 中卸载并稍后重新安装时,它才会再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM