学习React(22) - 介绍Pure components

2023-11-07 09:30

本文主要是介绍学习React(22) - 介绍Pure components,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天,就介绍一下pure components, 博主也不知道怎么翻译成中文才正确,就先用英文吧!

这个得创建三个文件:
第一个文件

// PureComponent.js 文件
import React, { PureComponent } from 'react'class PureComp extends PureComponent {render() {console.log("Pure component")return (<div>Pure Component {this.props.name}</div>)}
}export default PureComp

第二个文件

// RegularComponent.js 文件
import React, { Component } from 'react'class RegularComponent extends Component {render() {console.log("This is the Regular component")return (<div>Regular Component {this.props.name}</div>)}
}export default RegularComponent

第三个文件

// ParentComponent.js 文件
import React, { Component } from 'react'
import PureComp from './PureComponent'
import RegComp from './RegularComponent'class ParentComponent extends Component {constructor(props) {super(props)this.state = {name: "World"}}componentDidMount() {setInterval(() => {this.setState({name: "World"})}, 2000)}render() {console.log("*******This is the parent component*********")return (<div>Parent Component<RegComp name={this.state.name}/><PureComp name={this.state.name}/></div>)}
}export default ParentComponent

在App.js 文件里

// App.js 文件
import React from 'react';
import './App.css';
import Parentcom from './ParentComponent'function App() {return (<div className="App"><Parentcom/></div>);
}export default App;

结果如下:
在这里插入图片描述
在Chrome的console里显示:
在这里插入图片描述
Pure Component 只出现一次


Regular Component: A regular component does not implement the shouldComponentUpdate method. It always returns true by default.

Pure Component: A pure component on the other hand implements shouldComponentUpdate with a shallow props and state comparison.


Shallow Comparison (SC)
Primitive Types: a (SC) b returns true if a and b have the same value and are of the same type.

Complex Types: a (SC) b returns true if a and b reference the exact same object.


Pure Component:
A pure component implements shouldComponentUpdate with a shallow prop and state comparison.
Pure components by preventing unnecessary renders can give you a performance boost certain scenarios.


总结:
We can create a component by extending the pureComponent class.

A pureComponent implements the shouldComponentUpdate lifecycle method by performing a shallow comparison on the props and state of the component.

If there is no difference, the component is not re-rendered-performance boost.

It is a good idea to ensure that all the children components are also pure to avoid unexpected behavior.

Never mutate the state. Always return a new object that reflects the new state.


如果觉得不错的话,就用点赞来代替五星好评吧!

这篇关于学习React(22) - 介绍Pure components的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/362733

相关文章

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,