react生命周期

React class components can have hooks for several lifecycle events.

React类组件可以具有多个生命周期事件的挂钩。

Hooks allow function components to access them too, in a different way.

挂钩允许功能组件也以不同的方式访问它们。

During the lifetime of a component, there’s a series of events that gets called, and to each event you can hook and provide custom functionality.

在组件的生存期内,会调用一系列事件,对于每个事件,您都可以挂钩并提供自定义功能。

What hook is best for what functionality is something we’re going to see here.

什么挂钩最适合什么功能,我们将在这里看到。

First, there are 3 phases in a React component lifecycle:

首先,React组件生命周期分为三个阶段:

  • Mounting安装
  • Updating更新中
  • Unmounting正在卸载

Let’s see those 3 phases in detail and the methods that get called for each.

让我们详细了解这三个阶段以及每个阶段调用的方法。

安装 (Mounting)

When mounting you have 4 lifecycle methods before the component is mounted in the DOM: the constructor, getDerivedStateFromProps, render and componentDidMount.

挂载时,在将组件挂载到DOM之前,有4种生命周期方法: constructor getDerivedStateFromPropsrendercomponentDidMount

建设者 (Constructor)

The constructor is the first method that is called when mounting a component.

构造器是安装组件时调用的第一个方法。

You usually use the constructor to set up the initial state using this.state = ....

通常,您可以使用构造函数通过this.state = ...设置初始状态。

getDerivedStateFromProps() (getDerivedStateFromProps())

When the state depends on props, getDerivedStateFromProps can be used to update the state based on the props value.

当状态取决于道具时,可以使用getDerivedStateFromProps根据道具值更新状态。

It was added in React 16.3, aiming to replace the componentWillReceiveProps deprecated method.

它是在React 16.3中添加的,旨在替换componentWillReceiveProps不推荐使用的方法。

In this method you haven’t access to this as it’s a static method.

在此方法中,您无法访问this因为它是静态方法。

It’s a pure method, so it should not cause side effects and should return the same output when called multiple times with the same input.

这是一个纯方法,因此它不会引起副作用,并且在使用同一输入多次调用时应返回相同的输出。

Returns an object with the updated elements of the state (or null if the state does not change)

返回具有状态更新后的元素的对象(如果状态不变,则返回null)

render() (render())

From the render() method you return the JSX that builds the component interface.

从render()方法中,您返回构建组件接口的JSX。

It’s a pure method, so it should not cause side effects and should return the same output when called multiple times with the same input.

这是一个纯方法,因此它不会引起副作用,并且在使用同一输入多次调用时应返回相同的输出。

componentDidMount() (componentDidMount())

This method is the one that you will use to perform API calls, or process operations on the DOM.

该方法是用于执行API调用或处理DOM上的操作的方法。

更新中 (Updating)

When updating you have 5 lifecycle methods before the component is mounted in the DOM: the getDerivedStateFromProps, shouldComponentUpdate, render, getSnapshotBeforeUpdate and componentDidUpdate.

更新时,在组件安装到DOM中之前有5种生命周期方法: getDerivedStateFromPropsshouldComponentUpdaterendergetSnapshotBeforeUpdatecomponentDidUpdate

getDerivedStateFromProps() (getDerivedStateFromProps())

See the above description for this method.

有关此方法,请参见上面的描述。

shouldComponentUpdate() (shouldComponentUpdate())

This method returns a boolean, true or false. You use this method to tell React if it should go on with the rerendering, and defaults to true. You will return false when rerendering is expensive and you want to have more control on when this happens.

此方法返回boolean, truefalse 。 您可以使用此方法来告知React是否应继续进行重新渲染,并且默认为true 。 当重新渲染的成本很高时,您将返回false ,并且您希望对何时发生重新控制。

render() (render())

See the above description for this method.

有关此方法,请参见上面的描述。

getSnapshotBeforeUpdate() (getSnapshotBeforeUpdate())

In this method you have access to the props and state of the previous render, and of the current render.

在此方法中,您可以访问先前渲染以及当前渲染的道具和状态。

Its use cases are very niche, and it’s probably the one that you will use less.

它的用例非常小众,可能是您将减少使用的一种。

componentDidUpdate() (componentDidUpdate())

This method is called when the component has been updated in the DOM. Use this to run any 3rd party DOM API or call APIs that must be updated when the DOM changes.

当组件已在DOM中更新时,将调用此方法。 使用它来运行任何第三方DOM API或在DOM更改时必须更新的调用API。

It corresponds to the componentDidMount() method from the mounting phase.

从安装阶段开始,它对应于componentDidMount()方法。

正在卸载 (Unmounting)

In this phase we only have one method, componentWillUnmount.

在这一阶段,我们只有一个方法componentWillUnmount

componentWillUnmount() (componentWillUnmount())

The method is called when the component is removed from the DOM. Use this to do any sort of cleanup you need to perform.

从DOM中删除组件时,将调用此方法。 使用此功能可以执行您需要执行的任何类型的清理。

遗产 (Legacy)

If you are working on an app that uses componentWillMount, componentWillReceiveProps or componentWillUpdate, those were deprecated in React 16.3 and you should migrate to other lifecycle methods.

如果您正在使用使用componentWillMountcomponentWillReceivePropscomponentWillUpdate的应用程序,则在React 16.3中已弃用了这些应用程序,您应该迁移到其他生命周期方法。

翻译自: https://flaviocopes.com/react-lifecycle-events/

react生命周期

react生命周期_React生命周期事件相关推荐

  1. react取消捕获_React中阻止事件冒泡的问题详析

    前言 最近在研究react.redux等,网上找了很久都没有完整的答案,索性自己整理下,这篇文章就来给大家介绍了关于React阻止事件冒泡的相关内容,下面话不多说了,来一起看看详细的介绍吧 在正式开始 ...

  2. .net组件开发系列之武术系列 武术招数 控件生命周期与控件事件机制

    .net组件开发系列之武术系列 武术招数 控件生命周期与控件事件机制一.控件生命周期 先回述上篇,可能表述没有不清晰,也可能跨度大了点,好的,我们来一个循序渐进过程,大家都知道,武术都有招术的,先出什 ...

  3. React 重温之 组件生命周期

    生命周期 任何事物都不会凭空产生,也不会无故消亡.一个事物从产生到消亡经理的各个阶段,我们称之为 生命周期. 具体到我们的前端组件上来,一个组件的生命周期可以大体分为创建.更新.销毁这个三个阶段. 本 ...

  4. React Component Lifecycle(生命周期)

    生命周期 所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命周期,是合理开发的关键.RN 组件的生命周期整理如下图: 如图,可以把组件生命周期大致分为三个阶段: 第一阶段:是组件第一 ...

  5. React新旧版本生命周期对比

    React新旧版本生命周期对比 ❶ 过时生命周期: ① componentWillMount ② componentWillReceiveProps ③ componentWillUpdate ❷ 即 ...

  6. react 合并数组_React快速上手

    Quick Start 可以使用 create-react-app 快速创建 react 项目,里面已经封装好了常用的 webpack 的配置.这个工具其实就和 Vue 里面的 vue-cli 一样, ...

  7. 计组高分笔记:【05】中央处理器 「CPU细节 | 取指周期 | 间址周期 | 执行周期 | 中断周期 | 数据通路 | 硬布线控制器 | 微程序控制器 | 指令流水线 | LOAD | STORE」

    文章目录 1. CPU的功能和基本结构 1.1 CPU的组成 1.1.1 运算器的基本组成 1.2.2 控制器的基本组成 1.2.3 CPU的总图 2. 指令执行过程 2.1 指令周期 2.2 指令的 ...

  8. 【遥感】时间分辨率:轨道周期 / 运行周期 / 重复周期 / 轨道重访周期 / 重访周期 概念

    这些概念在不同的课本上有不同的教法,我曾经苦恼了好几个半天.现在按照我的理解整理如下,如有错,请指正.不过我觉得只要理解这几个概念究竟具体定义是什么就好了,不必纠结具体应该叫什么名字. 轨道周期 / ...

  9. 这就是为什么我们需要在React的类组件中绑定事件处理程序

    by Saurabh Misra 索拉·米斯拉(Saurabh Misra) 这就是为什么我们需要在React的类组件中绑定事件处理程序 (This is why we need to bind ev ...

最新文章

  1. 有关GBDT(Gradient Boosting Decison Tree,梯度提升树)
  2. 如何在sqlite3连接中创建并调用自定义函数
  3. 使用Dockerfile构建镜像
  4. PostgreSQL-JDBC疑似bug:部分接口参数的表名、列名必须全部小写
  5. 1 图片channels_【深度学习】卷积神经网络图片分类案例(pytorch实现)
  6. html5 table的表头拖动,可拖动table表头的实现
  7. python 对象锁_也许你对 Python GIL 锁的理解是 错的。
  8. windbg linux内核调试,windbg调试虚拟机XP系统
  9. 修饰性形容词-big、good、high_35
  10. 一主多备几影响 oracle,Oracle 10g 一主多备的搭建技巧
  11. _DataStructure_C_Impl:稀疏矩阵三元组
  12. GPO组策略 权限处理之原则
  13. matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
  14. linux ip转发 丢包,sendto频率过快导致发送丢包
  15. Python 在问答频道中刷题积累到的小技巧(二)
  16. Linux 火狐 flash更新
  17. 公网ip和域名是干嘛的?域名由谁管理的?
  18. LQ0266 巧排扑克牌【模拟】
  19. B. Applejack and Storages(数学+STL的使用) Codeforces Round #662 (Div. 2)
  20. pap认证过程_思科课件8、PAP认证、CHAP认证

热门文章

  1. golang原生库mime/multipart上传formdata文件的一个小坑unexpected EOF
  2. LeetCode 365. 水壶问题
  3. 真人演示——怎样练腹肌最快最有效听语音
  4. 英雄联盟服务器维护裁决之地,杜绝演员!LOL裁决之地开启审判天使系统
  5. [英语语法]句法之状语从句
  6. 第四十五天 百度地图定位SDK
  7. python实现调用百度词典接口实现英译汉、汉译英
  8. 关于rac环境中的alter ext进程名, begin now
  9. VMware三种联网方式
  10. 02_JavaScript数据结构与算法(二)数组