【react】---pureComponent的理解
一、pureComponent的理解
pureComponent表示一个纯组件,可以用来优化react程序。减少render函数渲染的次数。提高性能
pureComponent进行的是浅比较,也就是说如果是引用数据类型的数据,只会比较不是同一个地址,而不会比较这个地址里面的数据是否一致
浅比较会忽略属性和或状态突变情况,其实也就是数据引用指针没有变化,而数据发生改变的时候render是不会执行的。如果我们需要重新渲染那么就需要重新开辟空间引用数据
好处:
当组件更新时,如果组件的props或者state都没有改变,render函数就不会触发。省去虚拟DOM的生成和对比过程,达到提升性能的目的。具体原因是因为react自动帮我们做了一层浅比较
二、例子
import React, { PureComponent } from "react";
export default class List extends PureComponent{
constructor(){
super();
this.state = {
userInfo:"李四",
arr:[]
}
this.handleAdd = this.handleAdd.bind(this);
this.handleModify = this.handleModify.bind(this);
}
render(){
let {userInfo,arr} = this.state;
return (
<div>
<h3>{userInfo}</h3>
<ul>
{
arr.map((item,index)=>(
<li>{item}</li>
))
}
</ul>
<button onClick={this.handleAdd}>添加</button>
<button onClick={this.handleModify}>修改</button>
</div>
)
}
handleAdd(){
//render函数不会执行 因为newArr还是引用这arr的地址 地址没有发生改变
let newArr = this.state.arr;
newArr.push("姓名");
this.setState({
arr:newArr
})
}
handleModify(){
//会执行 因为会做浅比较
this.setState({
userInfo:"张三"
})
}
}
三、使用场景
1、PureComponent一般会用在一些纯展示组件上。切结props和state不能使用同一个引用
2、在通过PureComponent进行组件的创建的时候不能够在写shouldComponentUpdate. 否则会引发警告
【react】---pureComponent的理解的更多相关文章
- React.Component 与 React.PureComponent(React之性能优化)
前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...
- [React] React.PureComponent
React.PureComponent is similar to React.Component. The difference between them is that React.Compone ...
- React.Component 和 React.PureComponent 、React.memo 的区别
一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...
- React PureComponent All In One
React PureComponent All In One import React, { // useState, // useEffect, // Component, PureComponen ...
- 抛开react,如何理解virtual dom和immutability
去年以来,React的出现为前端框架设计和编程模式吹来了一阵春风.很多概念,无论是原本已有的.还是由React首先提出的,都因为React的流行而倍受关注,成为大家研究和学习的热点.本篇分享主要就聚焦 ...
- React 和 Redux理解
学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...
- 对 React Context 的理解以及应用
在React的官方文档中,Context被归类为高级部分(Advanced),属于React的高级API,但官方并不建议在稳定版的App中使用Context. 很多优秀的React组件都通过Conte ...
- [React] PureComponent in React
In this lesson, you will learn how to use PureComponent in React to reduce the number of times your ...
- 【React】360- 完全理解 redux(从零实现一个 redux)
点击上方"前端自习课"关注,学习起来~ 前言 记得开始接触 react 技术栈的时候,最难理解的地方就是 redux.全是新名词:reducer.store.dispatch.mi ...
随机推荐
- 使用lightProbe来模拟动态物体的照明shader
VertexLit path中读取lightProbe烘焙信息: Shader "VertexLitProbe" { Properties { _MainTex ("Ba ...
- Ubuntu上安装git和创建工作区和提交文件!!!
1.安装git: sudo apt-get install git 2.创建工作区: 创建一个文件夹,sudo mkdir 文件文件夹.告诉git这是个工作区文件夹,sudo git init 文件夹 ...
- eclipse中的yaml插件
现在spring中推荐使用yaml格式的配置文件,扩展名为yml 在eclipse中编辑的话,可以安装yaml编辑插件,在Eclipse Marketpalce可以搜到两款: YEdit的推荐指数38 ...
- hive列转行
一.问题 hive如何将 a b a b a b c d c d c d 变为: a b ,, c d ,, 二.数据 test.txt cat column_row.txt a,b, a,b, a, ...
- Spark机器学习(3):保序回归算法
保序回归即给定了一个无序的数字序列,通过修改其中元素的值,得到一个非递减的数字序列,要求是使得误差(预测值和实际值差的平方)最小.比如在动物身上实验某种药物,使用了不同的剂量,按理说剂量越大,有效的比 ...
- Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点( FROM IBM)
Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包括主菜单(Main Menu),视图 / ...
- Spark 核心篇-SparkContext
本章内容: 1.功能描述 本篇文章就要根据源码分析SparkContext所做的一些事情,用过Spark的开发者都知道SparkContext是编写Spark程序用到的第一个类,足以说明SparkCo ...
- [svc]linux下网桥-docker网桥
网桥和交换机 2口交换机=网桥 交换机: 工作在数据链路层,根据源mac学习(控制层),目的mac转发(数据层). linux的网卡 vmware workstation中的桥接 参考: http:/ ...
- JS 日期转换,格式化等常用的函数定义
//判断字符串是否日期格式 function isDate(val) { return new Date(val) != "Invalid Date"; } //日期格式化 fun ...
- LeetCode: First Missing Positive 解题报告
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...