学习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

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路