React.Component 和 React.PureComponent 、React.memo 的区别
一 结论
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 的区别的更多相关文章
- React性能优化之PureComponent 和 memo使用分析
前言 关于react性能优化,在react 16这个版本,官方推出fiber,在框架层面优化了react性能上面的问题.由于这个太过于庞大,我们今天围绕子自组件更新策略,从两个及其微小的方面来谈rea ...
- 转载 React.createClass 对决 extends React.Component
先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...
- 002-and design-dva.js 知识导图-01JavaScript 语言,React Component
一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react ...
- React.js Tutorial: React Component Lifecycle
Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...
- 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 ...
- React源码解析之React.Children.map()(五)
一,React.Children是什么? 是为了处理this.props.children(this.props.children表示所有组件的子节点)这个属性提供的工具,是顶层的api之一 二,Re ...
- [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 ...
- React.Component 与 React.PureComponent(React之性能优化)
前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...
- React源码 React.Component
React中最重要的就是组件,写的更多的组件都是继承至 React.Component .大部分同学可能都会认为 Component 这个base class 给我们提供了各种各样的功能.他帮助我们去 ...
随机推荐
- golang "%p"学习记录随笔
对于获取slice的指针地址, 通过unsafe.Pointer 和 "%p"占位符两种方式得到的地址是不同的 s := make([]int, 1) t.Log(unsafe.P ...
- 9.Kafka API使用
- Eclipse安装AmaterasUML插件问题
为了画UML图,我想在Eclipse(版本Version: Oxygen Release (4.7.0))安装AmaterasUML,第一步,安装GEF - http://download.eclip ...
- 企业项目实战 .Net Core + Vue/Angular 分库分表日志系统二 | 简单的分库分表设计
教程预览 01 | 前言 02 | 简单的分库分表设计 03 | 控制反转搭配简单业务 04 | 强化设计方案 05 | 完善业务自动创建数据库 06 | 最终篇-通过AOP自动连接数据库-完成日志业 ...
- Go-注释
什么是注释? 注释是给开发人员看的,目的是降低开发人员阅读代码的时间成本和代码阅读困难程度 Go-注释内容 1. 包注释,位于某个包下Go程序文件的顶部 2. 函数注释,位于Go函数的头部 3. 代码 ...
- 决策树减支问题(优化)dfs减支问题
#include <iostream>#include <cstdio>using namespace std;int mem[200];//开记忆数组int fib(int ...
- MySQL系列:Docker安装 MySQL提示错误:Access denied for user'root'@'localhost' (using password:yes)
问题: 解决方法: 在my.conf文件里配置 [mysqld] skip-grant-tables
- 035 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 02 多重if结构
035 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 02 多重if结构 本文知识点:Java中的多重if结构 选择结构回顾 if选择结构 注意: 1.条 ...
- matlab中repmat函数的用法
转载:https://blog.csdn.net/facetosea1/article/details/83573859 B = repmat(A,m,n)B = repmat(A,[m n])B = ...
- 温故知新————c++ 多态
参考: 1. https://blog.csdn.net/weixin_42678507/article/details/89414998 (直接说明原理) 2 .https://www.cnblo ...