hn-failte's blog hn-failte's blog
首页
  • 前端文章

    • JavaScript
    • Vue
    • React
    • Webpack
    • 混合开发
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《React》笔记
    • 《TypeScript 从零实现 axios》
    • 《Git》学习笔记
    • TypeScript笔记
    • JS设计模式总结笔记
  • HTML&CSS
  • HTML
  • CSS
  • CSS预处理
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 算法
  • 数据库
  • 操作系统
  • 工具
  • 学习
  • 面试
  • 心情杂货
  • 前端相关
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

hn-failte

前端cv仔
首页
  • 前端文章

    • JavaScript
    • Vue
    • React
    • Webpack
    • 混合开发
  • 学习笔记

    • 《JavaScript教程》笔记
    • 《JavaScript高级程序设计》笔记
    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《React》笔记
    • 《TypeScript 从零实现 axios》
    • 《Git》学习笔记
    • TypeScript笔记
    • JS设计模式总结笔记
  • HTML&CSS
  • HTML
  • CSS
  • CSS预处理
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 算法
  • 数据库
  • 操作系统
  • 工具
  • 学习
  • 面试
  • 心情杂货
  • 前端相关
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Webpack

  • React

    • React中context的用法
    • React中Hooks的使用
    • React之immutable的使用
    • React性能优化
      • 一、事件的优化
      • 二、数据结构优化
      • 三、渲染优化
      • 四、组件优化
    • Redux的使用
    • 为什么项目不使用Redux
    • React配置Webpack开发环境
    • 谈谈Vue/React中的虚拟DOM(VDOM)与Key值
  • JavaScript

  • Vue

  • 混合开发

  • 学习笔记

  • 微信小程序

  • 前端
  • React
hn-failte
2019-03-09

React性能优化

# React性能优化

# 一、事件的优化

1、构造函数中声明

export default MyCom extends Component{
    constructor(){
        super()
        this.fun = this.fun.bind(this)
    }
    render(){
        return(
            <div onClick={this.fun}></div>
        )
    }
	fun(){
        console.log("fun")
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

此中方式只会在构造函数中创建一次,性能最好,但无法传值

2、在组件中声明

export default MyCom extends Component{
    render(){
        return(
            <div onClick={this.fun.bind(this, arg)}></div>
    	)
	}
    fun(){
        console.log("fun")
    }
}
1
2
3
4
5
6
7
8
9
10

该方法可以解决以上不能传参的问题,但每次在render时都会重新执行一遍函数

3、在渲染内容中使用箭头函数

export default MyCom extends Component{
    render(){
        return(
            <div onClick={(arg)=>this.fun(arg)}></div>
    	)
	}
    fun(){
        console.log("fun")
    }
}
1
2
3
4
5
6
7
8
9
10

该方法每次执行render时都会重新生成箭头函数,极不推荐

# 二、数据结构优化

使用Immutable对Reducer的数据进行管理

import immutable from "immutable"
const defaulteState = immutable.fromJS({
    list: []
})

export default (state = defaultState, action)=>{
    switch(action.type){
        case "Add":
            return state.updateIn(["list"], list=>list.push(action.payload))
        default:
            return state
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

使用该方法可以减少在修改值时无需创建新的对象,减少内存的消耗,从而达到性能优化的目的

# 三、渲染优化

shuoldComponentUpdate的优化

BaseComponent.js

import React from "react";
import {is} from "immutable";
export default class BaseComponent extends React.Component{
	shouldComponentUpdate(nextProps, nextState, nextContext) {
        let thisState = this.state || {};
        let thisProps = this.props || {};
        nextProps = nextProps || {};
        nextState = nextState || {};

        if(Object.keys(thisState).length != Object.keys(nextState).length
           || Object.keys(thisProps).length != Object.keys(nextProps).length){
            return true;
        }

        for(var key in nextProps){
            if(!is(nextProps[key],thisProps[key])){
                return true;
            }
        }

        for(var key in nextState){
            if(!is(nextState[key],thisState[key])){
                return true;
            }
        }

        return false;

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

MyComponent.js

class MyComponent extends BaseComponent{
    render(){
        return(<div></div>)
    }
}
1
2
3
4
5

根据比较的结果是否一致,来判定是否需要重新渲染

# 四、组件优化

PureComponent是纯组件

特点:

  • 一般作为UI组件使用
  • 会对数据进行一次浅比较,只会关注数据的地址是否改变,若未改变则不会渲染
  • 使用了该组件后,禁止使用shouldCoponentUpdate,否则会破坏PureCoponent的规则
编辑 (opens new window)
#R#e#a#c#t
上次更新: 2021/08/05, 12:37:41
React之immutable的使用
Redux的使用

← React之immutable的使用 Redux的使用→

最近更新
01
基于 Taro 的微信小程序优化指南
02-16
02
搭建一个极简混合开发架构
08-03
03
使用State Hook
04-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 hn-failte | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式