前言

先说说 shouldComponentUpdate

提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看出来,这个函数是用来控制组件是否应该被更新的。

React.PureComponent 通过prop和state的浅对比来实现shouldComponentUpate()。

简单来说,这个生命周期函数返回一个布尔值。

如果返回true,那么当props或state改变的时候进行更新;

如果返回false,当props或state改变的时候不更新,默认返回true。

(这里的更新不更新,其实说的是执不执行render函数,如果不执行render函数,那自然该组件和其子组件都不会重新渲染啦)

重写shouldComponentUpdate可以提升性能,它是在重新渲染过程开始前触发的。当你明确知道组件不需要更新的时候,在该生命周期内返回false就行啦!

下面是一个重写shouldComponentUpdate的例子:

class CounterButton extends React.Component {
state={
count: 1
}
shouldComponentUpdate(nextProps, nextState) {
const {color}=this.props;
const {count}=this.state;
if (color !== nextProps.color) {
return true;
}
// 重写shouldComponentUpdate若将此处count相关逻辑注释则count变化页面不渲染
// if (count !== nextState.count) {
// return true;
// }
return false;
} render() {
const {color}=this.props;
const {count}=this.state;
return (
<button
style={{color}}
onClick={() => this.setState(state => ({count: state.count + 1}))}
>
Count: {count}
</button>
);
}
}

React.Component 与 React.PureComponent

言归正传,接下来说我们今天要讨论的React.Component 与 React.PureComponent。

通常情况下,我们会使用ES6的class关键字来创建React组件:

class MyComponent extends React.Component {
// some codes here ...
}

但是,你也可以创建一个继承React.PureComponent的React组件,就像这样

class MyComponent extends React.PureComponent {
// some codes here
}

那么,问题来了,这两种方式有什么区别呢?

继承PureComponent时,不能再重写shouldComponentUpdate,否则会引发警告(报错截图就不贴了,怪麻烦的)

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

继承PureComponent时,进行的是浅比较,也就是说,如果是引用类型的数据,只会比较是不是同一个地址,而不会比较具体这个地址存的数据是否完全一致

class ListOfWords extends React.PureComponent {
render() {
return <div>{this.props.words.join(',')}</div>;
}
}
class WordAdder extends React.Component { state = {
words: ['adoctors','shanks']
}; handleClick = () => {
const {words} = this.state;
words.push('tom');
this.setState({words});
console.log(words)
} render() {
const {words}=this.state;
return (
<div>
<button onClick={this.handleClick}>click</button>
<ListOfWords words={words} />
</div>
);
}
}

上面代码中,无论你怎么点击按钮,ListOfWords渲染的结果始终没变化,原因就是WordAdder的word的引用地址始终是同一个。

浅比较会忽略属性或状态突变的情况,其实也就是,数据引用指针没变而数据被改变的时候,也不新渲染组件。但其实很大程度上,我们是希望重新渲染的。所以,这就需要开发者自己保证避免数据突变。

如果想使2中的按钮被点击后可以正确渲染ListOfWords,也很简单,在WordAdder的handleClick内部,将 const words = this.state.words;

改为const words = this.state.words.slice(0);(这时的words是在原来state的基础上复制出来一个新数组,所以引用地址当然变啦)

React.Component 与 React.PureComponent(React之性能优化)的更多相关文章

  1. React性能优化总结

    本文主要对在React应用中可以采用的一些性能优化方式做一下总结整理 前言 目的 目前在工作中,大量的项目都是使用react来进行开展的,了解掌握下react的性能优化对项目的体验和可维护性都有很大的 ...

  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的性能优化 - 代码拆分之lazy的使用方法

    我们在某些网站上肯定看到过这样一种现象,页面上图片只有你滚动到那个位置附近的时候才会加载,否则就只占了个位,这就是延迟加载最普遍的应用场景. 我们react框架进行开发的时候也是一样,没有使用的组件是 ...

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

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

  7. 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 ...

  8. React性能优化 PureComponent

    为什么使用? React15.3中新加了一个 PureComponent 类,顾名思义, pure 是纯的意思, PureComponent 也就是纯组件,取代其前身 PureRenderMixin  ...

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

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

随机推荐

  1. 【原】使用puppeteer爬虫下载Midi文件

    The Beatles 乐队的 Midi文件下载地址 puppeteer官方github地址 midi文件爬取示例代码github地址 1.安装npm 参考:安装npm及cnpm(Windows) 修 ...

  2. (转)libvirt 部分API 介绍

    感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正! 如果转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  3. java 蓝桥杯算法提高 出现次数最多的整数

    思路:其实这道题不是太难,但是这个题太坑了,提交了好多次都不是100,后来才知道,一定一定要在输入数组数据之前先判断一下输进去的n的范围,一定一定要注意,否则就是跟我下面的图片一样的效果了,泪奔~ 问 ...

  4. 【UVA11419 训练指南】我是SAM 【二分图最小覆盖,最小割】

    题意 给出一个R*C大小的网格,网格上面放了一些目标.可以在网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算最少需要多少子弹,各从哪些位置发射,才能把所有目 ...

  5. Python中super详解

    转至:https://mozillazg.com/2016/12/python-super-is-not-as-simple-as-you-thought.html 说到 super, 大家可能觉得很 ...

  6. jQuery开发者眼中的AngularJS

    文章来源:http://blog.jobbole.com/76265/ AngualrJS是一个很贴心的web应用框架.它有很不错的官方文档和示例:经过在现实环境中的测试著名的TodoMVC proj ...

  7. [C++] Variable/Hex conversion

    程序编译链接原理预处理:.c -> .i gcc -E hello.c -o hello.i 编译:.i / .c -> .sgcc -S hello.i -o hello.s 汇编:.s ...

  8. configparser模块读写ini配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  9. jquery 常用工具方法

    inArray(value, array [, fromIndex ])方法类似于原生javascript的indexOf()方法,没有找到匹配元素时它返回-1.如果数组第一个元素匹配参数,那么$.i ...

  10. ios7 - Custom UItabbar has a gap in the bottom

    3down votefavorite   Im trying to create a custom UITabbar using images for the selected and unselec ...