美文网首页
React.PureComponent & React.memo

React.PureComponent & React.memo

作者: 隐号骑士 | 来源:发表于2019-08-15 00:10 被阅读0次

问题: react中如何尽量减少不必要的渲染

shouldComponentUpdate()

它是react的一个生命周期

图片来自:https://segmentfault.com/a/1190000016494335

组件state或props被更新时可以通过这个生命周期判断是否继续渲染。

它接受两个参数nextPropsnextState,返回一个布尔值。

若不在代码中声明该生命周期,react默认的处理是:

  shouldComponentUpdate(nextProps, nextState) {
    return true;
  }

编写代码的时候可以通过该生命周期来优化渲染

    shouldComponentUpdate(nextProps, nextState) {
        if (this.props.name === nextProps.name) {
            return false;
        }
        return true;
    }

React.PureComponent

大部分情况下,你可以使用 React.PureComponent 来代替手写 shouldComponentUpdate

以下两者效果相同:

import React from 'react';

class Child extends React.Component {
    shouldComponentUpdate(nextProps, nextState) {
        if (this.props.name === nextProps.name) {
            return false;
        }
        return true;
    }
    render() {
        console.log('render')
        return (
            <div>
                child,{this.props.name}
            </div>
        );
    }
}

export default Child;

import React from 'react';

class Child extends React.PureComponent {
    render() {
        console.log('render')
        return (
            <div>
                child,{this.props.name}
            </div>
        );
    }
}

export default Child;

React.PureComponent只进行浅比较(shallowEqual):

react shallowEqual 源码

const hasOwn = Object.prototype.hasOwnProperty

function is(x, y) {
  if (x === y) {
    return x !== 0 || y !== 0 || 1 / x === 1 / y
  } else {
    return x !== x && y !== y
  }
}

export default function shallowEqual(objA, objB) {
  if (is(objA, objB)) return true

  if (typeof objA !== 'object' || objA === null ||
      typeof objB !== 'object' || objB === null) {
    return false
  }

  const keysA = Object.keys(objA)
  const keysB = Object.keys(objB)

  if (keysA.length !== keysB.length) return false

  for (let i = 0; i < keysA.length; i++) {
    if (!hasOwn.call(objB, keysA[i]) ||
        !is(objA[keysA[i]], objB[keysA[i]])) {
      return false
    }
  }

  return true
}

React.memo()

它是React 16.8 的新特性之一

React.memo 为高阶组件。它与 React.PureComponent 非常相似,但它适用于函数组件,但不适用于 class 组件。

使用方法:

function MyComponent(props) {
  /* 使用 props 渲染 */
}
function areEqual(prevProps, nextProps) {
  /*
  如果把 nextProps 传入 render 方法的返回结果与
  将 prevProps 传入 render 方法的返回结果一致则返回 true,
  否则返回 false
  */
}
export default React.memo(MyComponent, areEqual);

参考:https://www.imweb.io/topic/598973c2c72aa8db35d2e291

相关文章

  • 解读 React.memo

    介绍React.memo之前,先了解一下React.Component和React.PureComponent。 ...

  • React.memo

    1.基础使用 React.memo 相当于 React.pureComponent内部的shouldCompone...

  • 细读 React | PureComponet

    今天来聊一聊 React.Component、React.PureComponent、React.memo 的一些...

  • React.memo

    React.memo 为高阶组件。它与 React.PureComponent 非常相似,但它适用于函数组件,但不...

  • React中hooks之useCallback搭配memo

    React.memo类似于React.PureComponent,能对props做浅比较,防止组件无效的重复渲染 ...

  • React.PureComponent & React.memo

    问题: react中如何尽量减少不必要的渲染 shouldComponentUpdate() 它是react的一个...

  • 如何使用React.memo()

    目录 包装函数 PureComponent React.memo() React.memo() 与Redux 其他...

  • React.memo()

    React 16.6.0 正式发布 React.memo() React.memo() 是什么? React.me...

  • Hooks进阶

    React.memo、useMemo、useCallback 的区别 React.memo 是对传入的 props...

  • [React Hooks] 样例学习---useWhyDidYo

    前置知识 React.memo React.memo 是一个高阶组件。类似于 React.PureComponen...

网友评论

      本文标题:React.PureComponent & React.memo

      本文链接:https://www.haomeiwen.com/subject/czmnjctx.html