美文网首页
react的性能优化

react的性能优化

作者: 水晶草720 | 来源:发表于2022-04-07 17:20 被阅读0次

1.shouldComponentUpdate

控件组件自身或者子组件是否需要更新,尤其是在子组件非常多的情况下,需要进行优化。

 shouldComponentUpdate(nextProps, nextState) {
        if (JSON.stringify(this.state.text) !== JSON.stringify(nextState.text)) {
            return true
        }
        return false
       
    }

2.PureComponent

pureComponent 会帮你 比较新props跟旧的props,新的state 和老的 state(值相等,或者对象含有相同的属性 且属性值相等),决定shouldcomponentUpdate 返回true 或者 false,从而决定要不要呼叫 render function
//注意 如果你的state 或 props [永远都会变],那PureComponent 并不会比较快,因为shallowEqual 也需要花时间


import React, { PureComponent } from 'react'
export default class app extends PureComponent {
    state = {
        mytext:"11111"
    }
    render() {
      console.log('render')
    return (
        <div>
            <button onClick={() => this.setState({mytext:"2222"})}>click</button>
            {this.state.mytext}
      </div>
    )
    }
    getSnapshotBeforeUpdate() {
        console.log("getSnapshotBeforeUpdate")
        return 111;
    }

    componentDidUpdate(prevProps, prevState,value) {
        console.log("componentDidUpdate",value)
    }
   
}

相关文章

  • 【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 组件性能优化最佳实践 React 组件性能优化的核心是减少渲染真实 DOM 节点的频率,减少 Virt...

  • 05|React组件的性能优化

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

  • React 性能优化

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

  • React性能优化方案

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

  • react 框架性能优化

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

网友评论

      本文标题:react的性能优化

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