一 结论

  React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作。
React.PureComponent 是继承自Component,并且对重写了shouldComponentUpdate周期函数,对 state 和 props 做了浅层比较,当state 和 props 均没有改变时候,不会render,仅可以用在ClassComponent中
React.memo 功能同React.PureComponent,但React.memo 为高阶组件,既可以用在ClassComponent中 也可以用在 functionComponent中

二 React.Component

  需要手动编写shouldCompoentUpdate进行 props和state的比较判断是否需要渲染render
Class Sum extends React.Component {
constructor(props){
super(props);
this.state = {
num:0
}
}
shouldCompoentUpdate(nextProps,nextState){
if(nextPorps.sum === this.props.sum && nextState.num === this.state.num){
return false; // 当props 和state 值均没有改变时候,让render不渲染
}
return ture; // 其他情况设置成允许渲染
}
render(){
return (
<div>
<span>{this.state.num}</span>
<span>{this.props.sum}</span>
</div>
)
}
}

二 React.PureComponent 和 React.memo 的使用

Class Sum extends React.PureComponent {
// 会自动进行props 和 sate 的浅比较
} function Add (props){
return (
<div>sum = {props.y + props.x}</div>
)
}
export default React.memo(Add)

注意:当props 或者 state 为一个 Object类型时候,浅比较会失效 即 props ,state 发生改变,依旧会阻止render渲染。

此时可以运用的方法有

1 修改state时候,采用 object.assin({},)进行组件合并

2 采用 解构赋值进行浅拷贝赋值,这样props或者state 就不再为原来的值了,可以触发render刷新操作

3 当组件结构较复杂,存在较多 Object类型时候 建议改为使用React.Component

React.Component 和 React.PureComponent 、React.memo 的区别的更多相关文章

  1. React性能优化之PureComponent 和 memo使用分析

    前言 关于react性能优化,在react 16这个版本,官方推出fiber,在框架层面优化了react性能上面的问题.由于这个太过于庞大,我们今天围绕子自组件更新策略,从两个及其微小的方面来谈rea ...

  2. 转载 React.createClass 对决 extends React.Component

    先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...

  3. 002-and design-dva.js 知识导图-01JavaScript 语言,React Component

    一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react ...

  4. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  5. React Tutorial: Basic Concept Of React Component---babel, a translator

    Getting started with react.js: basic concept of React component 1 What is React.js React, or React.j ...

  6. React源码解析之React.Children.map()(五)

    一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...

  7. [React] Use React.memo with a Function Component to get PureComponent Behavior

    A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...

  8. React.Component 与 React.PureComponent(React之性能优化)

    前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...

  9. React源码 React.Component

    React中最重要的就是组件,写的更多的组件都是继承至 React.Component .大部分同学可能都会认为 Component 这个base class 给我们提供了各种各样的功能.他帮助我们去 ...

随机推荐

  1. 虚拟机系列 | JVM特点,基础结构与执行周期

    本文源码:GitHub·点这里 || GitEE·点这里 一.虚拟机简介 1.虚拟机概念 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整 ...

  2. 安装MySQL和出现的问题解决

    在Windows下安装mysql,注意自己的Windows是32位还是64位. MySQL官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载完之后,解压放 ...

  3. uni-app支付功能

    扫码查看原文 前言 近期一直在使用APP开发多端应用,IOS的APP.安卓的APP和H5网页,其中开发的APP使用到了微信和支付宝的支付,在此给大家分享出来,一起使用 前置条件: 开发环境:windo ...

  4. JQuery实现tab页

    用ul 和 div 配合实现tab 页 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="U ...

  5. mysql-10-union

    #进阶10:联合查询 /* union联合 将多条查询语句的结果合并成一个结果 语法: 查询1 union 查询2 union 查询3 ... 应用场景:要查询的结果来自于多个表,且多个表没有直接的连 ...

  6. MFC与QT区别

    转载  https://www.cnblogs.com/forever5325/p/9597649.html QT使用的编译器是MinGW,即Linux下的GCC移植到windows的版本:MFC使用 ...

  7. 国产化之路-统信UOS + Nginx + Asp.Net MVC + EF Core 3.1 + 达梦DM8实现简单增删改查操作

    专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...

  8. spring-boot-route(六)整合JApiDocs生成接口文档

    上一篇文章中介绍了使用Swagger生成接口文档,非常方便,功能也十分强大.如果非要说Swaager有什么缺点,想必就是注解写起来比较麻烦.如果我说有一款不用写注解,就可以生成文档的工具,你心动了吗? ...

  9. matlab中fft快速傅里叶变换

    视频来源:https://www.bilibili.com/video/av51932171?t=628. 博文来源:https://ww2.mathworks.cn/help/matlab/ref/ ...

  10. Python之路——变量

    什么是变量 #变量即变化的量,核心是"变"与"量"二字,变即变化,量即衡量状态. 为什么要有变量 #程序执行的本质就是一系列状态的变化,变是程序执行的直接体现, ...