react性能优化中,提到的就是通过 React.PureComponent 替换 React.Component 组件进行编程。

两个组件之间的不同主要就是PureComponent做了shouldComponentUpdate的优化。对props和state进行了第一层的值===比较, 并且对 context 的变化不进行判断。

通过查看React可以看到

 /**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @typechecks
*
*/ /*eslint-disable no-self-compare */ 'use strict'; var hasOwnProperty = Object.prototype.hasOwnProperty; /**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function is(x, y) {
// SameValue algorithm
if (x === y) {
// Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
// Added the nonzero y check to make Flow happy, but it is redundant
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
} /**
* Performs equality by iterating through keys on an object and returning false
* when any key has values which are not strictly equal between the arguments.
* Returns true when the values of all keys are strictly equal.
*/
function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
} if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
} var keysA = Object.keys(objA);
var keysB = Object.keys(objB); if (keysA.length !== keysB.length) {
return false;
} // Test for A's keys different from B.
for (var i = 0; i < keysA.length; i++) {
if (!hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
} return true;
} module.exports = shallowEqual;

shallowEqual

项目准备升级 React16,控制台出现了错误 shouldComponentUpdate should not be used when extending React.PureComponent.

一开始看很莫名其妙,明明组件没有自己写shouldComponentUpdate,怎么就报错了。

后来谷歌了下,原来是项目使用了mobx进行的状态管理。使用mox-react,对所有组件都通过 @observer 装饰器,以前也看过mobx-react源码,里面的确mixin了shouldComponentUpdate方法,优化了组件的性能

看下面的源代码

 function mixinLifecycleEvents(target) {
patch(target, "componentWillMount", true)
;["componentDidMount", "componentWillUnmount", "componentDidUpdate"].forEach(function (
funcName
) {
patch(target, funcName)
})
if (!target.shouldComponentUpdate) {
target.shouldComponentUpdate = reactiveMixin.shouldComponentUpdate
}
}
{
shouldComponentUpdate: function(nextProps, nextState) {
if (isUsingStaticRendering) {
console.warn(
"[mobx-react] It seems that a re-rendering of a React component is triggered while in static (server-side) mode. Please make sure components are rendered only once server-side."
)
}
// update on any state changes (as is the default)
if (this.state !== nextState) {
return true
}
// update if props are shallowly not equal, inspired by PureRenderMixin
// we could return just 'false' here, and avoid the `skipRender` checks etc
// however, it is nicer if lifecycle events are triggered like usually,
// so we return true here if props are shallowly modified.
return isObjectShallowModified(this.props, nextProps)
}
}
function isObjectShallowModified(prev, next) {
if (null == prev || null == next || typeof prev !== "object" || typeof next !== "object") {
return prev !== next
}
const keys = Object.keys(prev)
if (keys.length !== Object.keys(next).length) {
return true
}
let key
for (let i = keys.length - 1; i >= 0, (key = keys[i]); i--) {
if (next[key] !== prev[key]) {
return true
}
}
return false
}

经过测试,使用setState后,shouldComponentUpdate 的确 this.state!==nextState,但是对于this.props, nextProps的判断,还是存在一些差别的。

可以试下下面的比较

console.log(isObjectShallowModified(NaN, NaN), !shallowEqual(NaN, NaN));

var a = {a: 1}, b = {b: 1, __proto__: {a: 1}};
console.log(isObjectShallowModified(a, b), !shallowEqual(a, b));

对于一些比较简单的对象比较结果还是相同的,所以对于@observer的组件就改为了React.Component进行处理。

react与mox-react的shouldComponentUpdate 理解的更多相关文章

  1. 【React】383- React Fiber:深入理解 React reconciliation 算法

    作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...

  2. 手写React的Fiber架构,深入理解其原理

    熟悉React的朋友都知道,React支持jsx语法,我们可以直接将HTML代码写到JS中间,然后渲染到页面上,我们写的HTML如果有更新的话,React还有虚拟DOM的对比,只更新变化的部分,而不重 ...

  3. React 之 高阶组件的理解

    1.基本概念 高阶组件是参数为组件,返回值为新组件的函数. 2.举例说明 ① 装饰工厂模式 组件是 react 中的基本单元,组件中通常有一些逻辑(非渲染)需要复用处理.这里我们可以用高阶组件对组件内 ...

  4. react系列从零开始-react介绍

    react算是目前最火的js MVC框架了,写一个react系列的博客,顺便回忆一下react的基础知识,新入门前端的小白,可以持续关注,我会从零开始教大家用react开发一个完整的项目,也会涉及到w ...

  5. [React] 从零开始的react

    组件 1. 无状态组件 在React中,组件的名字必须用大写字母开头,而包含该组件定义的文件名也应该是大写字母(便于区分,也可以不是). 无状态组件是纯展示组件,仅仅只是用于数据的展示,只根据传入的p ...

  6. React 学习(一) ---- React Element /组件/JSX

    学习React的时候,你可能听到最多的就是要先学习webpack, babel,要先学会配置然后才能学react 等等,一堆的配置就把我们吓着了,根本就没有心情就学习react了.其实在最开始学习re ...

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

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

  8. React Native之React速学教程(下)

    概述 本篇为<React Native之React速学教程>的最后一篇.本篇将带着大家一起认识ES6,学习在开发中常用的一些ES6的新特性,以及ES6与ES5的区别,解决大家在学习Reac ...

  9. react新特性 react hooks

    本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...

随机推荐

  1. git使用笔记(十一)rebase

    By francis_hao    Oct 22,2017   git-rebase,改变commit的基础参照 概要 git rebase [-i | --interactive] [options ...

  2. LoadRunner中的IP欺骗

    应用程序服务器和网络设备使用IP地址来识别客户端.应用程序服务器通常会对来自同一计算机的客户端信息进行高速缓存. 网络路由器尝试对原信息和目标信息进行高速缓存以优化吞吐量.如果多个用户具有相同的IP地 ...

  3. apt-get update的hit和ign含义

    How do Ign and Hit affect apt-get update? From what I can see in the apt source code, "Ign" ...

  4. 数据分析侠A的成长故事

    数据分析侠A的成长故事 面包君  同学A:22岁,男,大四准备实习,计算机专业,迷茫期 作为一个很普通的即将迈入职场的他来说,看到周边的同学都找了技术开发的岗位,顿觉自己很迷茫,因为自己不是那么喜欢钻 ...

  5. Linux查看内核和系统版本

    1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe.d ...

  6. Hadoop 介绍

    1.Hadoop简介 Hadoop[hædu:p]实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高容错性的特点,并且设计用来部署在低 ...

  7. B树和TreeSet与TreeMap

    1. 此前二叉搜索树相关的内容我们均假设可以把整个数据结构存储在计算机的内存中,但是如果数据量过大时,必须把数据结构放在磁盘上,导致大O模型不在适用.目前计算机处理器每秒至少可以执行5亿条指令,磁盘访 ...

  8. asp.net SimpleImpersonation使用身份模拟访问局域网共享目录

    mvc中默认账户的权限很低,缺省情况下,ASP.NET应用程序以本机的ASPNET帐号运行,该帐号属于普通用户组,权限受到一定的限制,以保障ASP.NET应用程序运行的安全.但是有时需要某个ASP.N ...

  9. 【Foreign】Bumb [模拟退火]

    Bumb Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 1 5 1 4 Sample ...

  10. 【NOIP】提高组2015 跳石头

    [算法]二分查找 [题解]最小值最大化问题. 从1..l内二分枚举答案,将每个答案最少移开的石头数与最大移开数m比较. 精简写法学自:https://vijos.org/p/1981/solution ...