一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能

1.所谓浅比较(shallowEqual),即react源码中的一个函数,然后根据下面的方法进行是不是PureComponent的判断,帮我们做了本来应该我们在shouldComponentUpdate中做的事情

if (this._compositeType === CompositeTypes.PureClass) {
shouldUpdate = !shallowEqual(prevProps, nextProps) || ! shallowEqual(inst.state, nextState);
}

而本来我们做的事情如下,这里判断了state有没有发生变化(prop同理),从而决定要不要重新渲染,这里的函数在一个继承了Component的组件中,而这里this.state.person是一个对象,你会发现,在这个对象的引用没有发生变化的时候是不会重新render的(即下面提到的第三点),所以我们可以用shouldComponentUpdate进行优化,这个方法如果返回false,表示不需要重新进行渲染,返回true则重新渲染,默认返回true

  shouldComponentUpdate(nextProps, nextState) {
return (nextState.person !== this.state.person);
}

2.上面提到的某些情况下可以使用PureComponent来提升性能,那具体是哪些情况可以,哪些情况不可以呢,实践出真知

3.如下显示的是一个IndexPage组件,设置了一个stateisShow,通过一个按钮点击可以改变它的值,结果是:初始化的时候输出的是constructorrender,而第一次点击按钮,会输出一次render,即重新渲染了一次,界面也会从显示false变成显示true,但是当这个组件是继承自PureComponent的时候,再点击的时,不会再输出render,即不会再重新渲染了,而当这个组件是继承自Component时,还是会输出render,还是会重新渲染,这时候就是PureComponent内部做了优化的体现

4.同理也适用于stringnumber等基本数据类型,因为基本数据类型,值改变了就算改变了

import React, { PureComponent } from 'react';

class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
isShow: false
};
console.log('constructor');
}
changeState = () => {
this.setState({
isShow: true
})
};
render() {
console.log('render');
return (
<div>
<button onClick={this.changeState}>点击</button>
<div>{this.state.isShow.toString()}</div>
</div>
);
}
}

5.当这个this.state.arr是一个数组时,且这个组件是继承自PureComponent时,初始化依旧是输出constructorrender,但是当点击按钮时,界面上没有变化,也没有输出render,证明没有渲染,但是我们可以从下面的注释中看到,每点击一次按钮,我们想要修改的arr的值已经改变,而这个值将去修改this.state.arr,但是因为在PureComponent浅比较这个数组的引用没有变化所以没有渲染,this.state.arr也没有更新,因为在this.setState()以后,值是在render的时候更新的,这里涉及到this.setState()的知识

6.但是当这个组件是继承自Component的时候,初始化依旧是输出constructorrender,但是当点击按钮时,界面上出现了变化,即我们打印处理的arr的值输出,而且每点击一次按钮都会输出一次render,证明已经重新渲染,this.state.arr的值已经更新,所以我们能在界面上看到这个变化

import React, { PureComponent } from 'react';

class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
arr:['1']
};
console.log('constructor');
}
changeState = () => {
let { arr } = this.state;
arr.push('2');
console.log(arr);
// ["1", "2"]
// ["1", "2", "2"]
// ["1", "2", "2", "2"]
// ....
this.setState({
arr
})
};
render() {
console.log('render');
return (
<div>
<button onClick={this.changeState}>点击</button>
<div>
{this.state.arr.map((item) => {
return item;
})}
</div>
</div>
);
}
}

7.下面的例子用扩展运算符产生新数组,使this.state.arr的引用发生了变化,所以初始化的时候输出constructorrender后,每次点击按钮都会输出render,界面也会变化,不管该组件是继承自Component还是PureComponent

import React, { PureComponent } from 'react';

class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
arr:['1']
};
console.log('constructor');
}
changeState = () => {
let { arr } = this.state;
this.setState({
arr: [...arr, '2']
})
};
render() {
console.log('render');
return (
<div>
<button onClick={this.changeState}>点击</button>
<div>
{this.state.arr.map((item) => {
return item;
})}
</div>
</div>
);
}
}

8.上面的情况同样适用于对象的情况

二.PureComponent不仅会影响本身,而且会影响子组件,所以PureComponent最佳情况是展示组件

1.我们让IndexPage组件里面包含一个子组件Example来展示PureComponent是如何影响子组件的

2.父组件继承PureComponent,子组件继承Component时:下面的结果初始化时输出为constructorIndexPage renderexample render,但是当我们点击按钮时,界面没有变化,因为这个this.state.person对象的引用没有改变,只是改变了它里面的属性值所以尽管子组件是继承Component的也没有办法渲染,因为父组件是PureComponent,父组件根本没有渲染,所以子组件也不会渲染

3.父组件继承PureComponent,子组件继承PureComponent时:因为渲染在父组件的时候就没有进行,相当于被拦截了,所以子组件是PureComponent还是Component根本不会影响结果,界面依旧没有变化

4.父组件继承Component,子组件继承PureComponent时:结果和我们预期的一样,即初始化是会输出constructorIndexPage renderexample render,但是点击的时候只会出现IndexPage render,因为父组件是Component,所以父组件会渲染,但是
当父组件把值传给子组件的时候,因为子组件是PureComponent,所以它会对prop进行浅比较,发现这个person对象的引用没有发生变化,所以不会重新渲染,而界面显示是由子组件显示的,所以界面也不会变化

5.父组件继承Component,子组件继承Component时:初始化是会输出constructorIndexPage renderexample render,当我们第一次点击按钮以后,界面发生变化,后面就不再改变,因为我们一直把它设置为sxt2,但是每点击一次都会输出IndexPage renderexample render,因为每次不管父组件还是子组件都会渲染

6.所以正如下面第四条说的,如果stateprop一直变化的话,还是建议使用Component,并且PureComponent的最好作为展示组件

//父组件
import React, { PureComponent, Component } from 'react';
import Example from "../components/Example"; class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
person: {
name: 'sxt'
}
};
console.log('constructor');
}
changeState = () => {
let { person } = this.state;
person.name = 'sxt2';
this.setState({
person
})
};
render() {
console.log('IndexPage render');
const { person } = this.state;
return (
<div>
<button onClick={this.changeState}>点击</button>
<Example person={person} />
</div>
);
}
}
//子组件
import React, { Component } from 'react'; class Example extends Component { render() {
console.log('example render');
const { person } = this.props;
return(
<div>
{person.name}
</div>
);
}
}

三.若是数组和对象等引用类型,则要引用不同,才会渲染

四.如果prop和state每次都会变,那么PureComponent的效率还不如Component,因为你知道的,进行浅比较也是需要时间

五.若有shouldComponentUpdate,则执行它,若没有这个方法会判断是不是PureComponent,若是,进行浅比较

1.继承自Component的组件,若是shouldComponentUpdate返回false,就不会渲染了,继承自PureComponent的组件不用我们手动去判断propstate,所以在PureComponent中使用shouldComponentUpdate会有如下警告:

IndexPage has a method called shouldComponentUpdate(). shouldComponentUpdate should not be used when extending React.PureComponent. Please extend React.Component if shouldComponentUpdate is used.

也是比较好理解的,就是不要在PureComponent中使用shouldComponentUpdate,因为根本没有必要

转载:https://www.jianshu.com/p/c41bbbc20e65

React 的 PureComponent Vs Component的更多相关文章

  1. React Native(六)——PureComponent VS Component

    先看两段代码: export class ywg extends PureComponent { …… render() { return ( …… ); } } export class ywg e ...

  2. React.createClass和extends Component的区别

    React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...

  3. React Native 中的component 的生命周期

    React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...

  4. react优化--pureComponent

    shouldComponentUpdate的默认渲染 在React Component的生命周期中,shouldComponentUpdate方法,默认返回true,也就意味着就算没有改变props或 ...

  5. [React Native] Create a component using ScrollView

    To show a list of unchanging data in React Native you can use the scroll view component. In this les ...

  6. react中PureComponent浅对比策略

    PureComponent实现了Component中没有实现的shouComponentUpdata()方法,会对state和props进行一次浅对比,本文介绍一下浅对比策略 源码中,实现浅对比的函数 ...

  7. 【react】---pureComponent的理解

    一.pureComponent的理解  pureComponent表示一个纯组件,可以用来优化react程序.减少render函数渲染的次数.提高性能 pureComponent进行的是浅比较,也就是 ...

  8. [React] Write a stateful Component with the React useState Hook and TypeScript

    Here we refactor a React TypeScript class component to a function component with a useState hook and ...

  9. [React] Refactor a Class Component with React hooks to a Function

    We have a render prop based class component that allows us to make a GraphQL request with a given qu ...

随机推荐

  1. Mysql存储过程简单应用

    因为很久没写过存储过程了,语法也不记得了,靠百度后,解决了当前问题,这里就简单记录一下. CREATE PROCEDURE pro1() BEGIN DECLARE i int; DECLARE db ...

  2. [LC] 226. Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 /* ...

  3. 树的DFS

    Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. ...

  4. unittest(9)- 使用ddt给测试用例传参

    # 1. http_request.py import requests class HttpRequest: def http_request(self, url, method, data=Non ...

  5. Ionic3学习笔记(十二)拍照上传图片以及从相册选择图片上传

    本文为原创文章,转载请标明出处 目录 安装插件 导入 app.module.ts 创建 provider 更多 效果图 1. 安装插件 终端运行: ionic cordova plugin add c ...

  6. OpenStack入门

    云计算优势 降低成本,安全稳定,易扩展. 云计算三种服务模式 IaaS:基础设施即服务 IaaS(Infrastructure-as-a- Service):基础设施即服务.消费者通过Internet ...

  7. CentOS7安装Ceph

    CentOS 7 下安装Ceph-nautilus 本问主要记录在CentOS 7下如何安装Ceph-nautilus,安装过程中遇到的一些问题及解决方法. 实验准备 以下是本次实验所用到的机器(采用 ...

  8. IP不是万能药 为何有蜘蛛侠等大片的索尼要放弃电影

    ​ 为何有蜘蛛侠等大片的索尼要放弃电影"> 近年来,国内狂炒"IP"这一概念,比如动漫.网络文学.小说.游戏等,甚至围绕IP制造出内容矩阵.从一个IP延伸至多个领域 ...

  9. 游LeetCode一月之闲谈

    今年的2月比往常更长,不是因为比往年多了一天,而是被病毒隔离在家的日子显得十分漫长.如果再不给自己找点事情做的话,且不论身体方面的健康状况,精神方面可能也会有些隐忧.做为一名工程师,适时地读上几本平日 ...

  10. Python——2list和tuple类型

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...