美文网首页
react 性能优化

react 性能优化

作者: 一土二月鸟 | 来源:发表于2020-08-17 09:47 被阅读0次

减少render次数

  • 使用PureComponent,默认实现了shouldComponentUpdate方法,对state对象进行了浅比较。
  • 但如果state为复杂的数据结构,需小心setState可能会导致组件未被重新渲染。
  • PureComponent适用于一些state数据结构简单的组件,对于复杂的数据结构,如果使用了PureComponent,最好结合forceUpdate来使用,防止state改变了,但未被刷新。但这样使用PureComponent的意义就不大了。
import React from 'react';
import ReactDOM from 'react-dom';

class Index extends React.PureComponent {

  state = {
    data: {
      count: 1
    }
  }

  onClickBtn = () => {
    const data = this.state.data;
    data.count = 10;
    console.log(data);
    this.setState({
      data: data
    });
    this.forceUpdate();
  }

  render() {
    console.log('render...');
    return <>
      <p>{this.state.data.count}</p>
      <button onClick={this.onClickBtn}>add</button>
    </>;
  }

}

ReactDOM.render(<Index />, document.getElementById('root'));
  • 如果state为复杂的数据结构,但又想提升性能,可以结合immutable的equals快速对比数据的变化
import React from 'react';
import ReactDOM from 'react-dom';
import { Map } from 'immutable';

class Index extends React.Component { // 如果使用了shouldComponentUpdate,不推荐使用PureComponent,会被警告

  state = {
    data: Map({
      count: 1
    })
  }

  shouldComponentUpdate(nextProps, nextState) {
    if(this.state.data.equals(nextState.data)){ // 利用immutable的equals,可以快速比较复杂的数据是否发生了变化
      return false;
    } 
    return true;
  }

  onClickBtn = () => {
    this.setState({
      data: Map({
        count: 1
      })
    });
  }

  render() {
    console.log('render...');
    return <>
      <p>{this.state.data.get('count')}</p>
      <button onClick={this.onClickBtn}>add</button>
    </>;
  }

}

ReactDOM.render(<Index />, document.getElementById('root'));
  • 对于函数组件,可以使用React.memo生成高阶组件,此高阶组件默认会对props进行浅比较,从而决定是否要更新组件。
import React, { Component } from "react";
import ReactDom from "react-dom";

const Child = (props) => {
  console.log("child render....");

  return <p>{props.t}</p>;
};

const ChildMemo = React.memo(Child);

class Index extends Component {

  state = {
    t: 1
  }

  clickBtn = () => {
    this.setState(state => ({
      t: state.t
    }))
  }

  render() {
    return (
      <>
        <button onClick={this.clickBtn} >click</button>
        <ChildMemo t={this.state.t} />
      </>
    );
  }
}

ReactDom.render(<Index />, document.getElementById("root"));

相关文章

  • 【React.js 20】React性能优化

    React性能优化 React性能优化主要分三块: React 组件性能优化 属性传递优化针对单组件性能优化,很多...

  • 深入浅出React和Redux学习笔记(五)

    React组建的性能优化 性能优化的方法: 单个React组件的性能优化; 多个React组件的性能优化; 利用r...

  • Redux源码剖析

    前面写了《React组件性能优化》《Redux性能优化》《React-Redux性能优化》,但是都没有从这些框架的...

  • react性能优化

    React 性能优化 React 性能优化 | 包括原理、技巧、Demo、工具使用[https://juejin....

  • React-Redux性能优化

    前面写了两篇文章《React组件性能优化》《Redux性能优化》,分别针对React和Redux在使用上的性能优化...

  • react 框架性能优化

    react 框架性能优化 前端性能监控利器 1.Google Performance工具 2.react 性能查看...

  • React 性能优化

    React 性能优化 简单的 todo-list-demo 讲 React 性能优化不能光靠嘴说,得有一个 dem...

  • React性能优化方案

    React 性能优化 简单的 todo-list-demo 讲 React 性能优化不能光靠嘴说,得有一个 dem...

  • React 组件性能优化

    React 组件性能优化最佳实践 React 组件性能优化的核心是减少渲染真实 DOM 节点的频率,减少 Virt...

  • 05|React组件的性能优化

    本文主要是针对性能优化做介绍,对应的React本身性能就比较不错!要介绍的点如下: 单个React组件的性能优化 ...

网友评论

      本文标题:react 性能优化

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