博客
关于我
React 生命周期函数
阅读量:628 次
发布时间:2019-03-13

本文共 775 字,大约阅读时间需要 2 分钟。

一、初始化

在这个阶段,组件会设置自己的属性(props)和状态(state)。开发者通常会在这里初始化内部的数据源、事件处理器等。

二、安装

从React 16.3版本开始,componentWillMount 已经被列为不安全的方法。开发者可以通过在方法名前加上 UNSAFE_ 来维持兼容性。

已废除 的方法包括:

- componentWillMount:在组件将要挂载到页面时执行。 - componentWillReceiveProps:在组件第一次存在于 DOM 中时不会执行,但在二次渲染时会生效。 - componentWillUpdate:在 shouldComponentUpdate 之后执行。

%p> render 是组件挂载时(包括初次和次次渲染)的入口方法。

%p> componentDidMount:在组件完全挂载之后执行,适合进行初始化操作。

三、更新

在这阶段,组件会根据 props 或 state 的变化重新渲染。需要注意的是,componentWillReceiveProps 已经被废弃,建议使用 UNSAFE_ 前缀来使用这些方法。以下是相关步骤:

- componentWillReceiveProps:在组件已存在于 DOM 中时会执行。 - shouldComponentUpdate:在组件更新之前调用,用于控制是否渲染(建议返回 true 进行更新)。 - componentWillUpdate:在 shouldComponentUpdate 之后调用。 - render:渲染操作。 - componentDidUpdate:在组件更新完成后调用,适合执行在更新后需要的逻辑。

四、卸载

componentWillUnmount:当组件将被移除时会自动执行,用于释放资源或清理事件监听器。

转载地址:http://sapaz.baihongyu.com/

你可能感兴趣的文章
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>