美文网首页
React 性能优化

React 性能优化

作者: 我的钱包瘪瘪的 | 来源:发表于2020-03-31 22:25 被阅读0次

性能优化

antd按需加载

  • 方案一

        第一步: npm run eject
        
        第二步: npm i antd babel-plugin-import  // 安装
        
        第三步: webpack配置中修改 babel-loader option
        {
          "plugins": [
            ["import", {
              "libraryName": "antd",
              "libraryDirectory": "es",
              "style": "css" // `style: true` 会加载 less 文件
            }]
          ]
        }
        
    
  • 方案二

        第一步: npm install react-app-rewired@2.0.2-next.0 babel-plugin-import --save
        
        第二步: package.json的scripts启动命令,使用'react-app-rewired'取代'react-scripts'
        
        第三步:在根目录新增'config-overrides.js'文件
        
        第四部: npm start
    
  • config-overrides.js文件中代码

        // 不需要eject, 就能直接改写create-react-app的默认webpack配置
        const { injectBabelPlugin } = require("react-app-rewired");
        module.exports = function override(config, env) {
          config = injectBabelPlugin(
            // 在默认配置基础上注入
            // 插件名,插件配置
            ["import", { libraryName: "antd", libraryDirectory: "es", style: "css" }],
            config
          );
          return config;
        };
    
  • 以上任选一种,import {Button} from 'antd' 即可按需加载对应的js, css文件

容器组件和展示组件

  • 容器组件: 容器组件负责数据获取

  • 展示组件: 负责根据props显示信息

shouldcomponentUpdate

  • shouldComponentUpdate(15.5版本的优化)

  • 注意

        缺点: 每次需要重写shouldComponentUpdate, 进行数据的前后比较逻辑
    
  • 代码

        shouldComponentUpdate (nextProps) {
            if(nextProps.params === this.props.params) {
                return false
            }
            return true
        }
    

purecomponent

  • PureComponent(浅比较)

  • 注意

        1. 每次都是浅比较, 深比较时容易造成无限递归,性能反而下降
        
        2. 跟shouldComponentUpdate效果一致
        
        3. 代码建议是, 一般传入的props是值引用类型, 否则不好比较
    
  • 代码

        class Comp extends React.PureComponent {
            
        }
    
  • 代码原理

    [图片上传失败...(image-84a1f6-1585664719790)]

memo

  • memo高阶组件(浅对比)

  • 注意

        1. React.memo(16.6.0) 让函数式的组件也有PureComponent的功能
        
        2. memo传入展示组件,返回一个类似PureComponent包装过后的组件
    
  • 代码

        const Comment = React.memo((props) => (
            <p>{props.name}</p>
        ));
    

相关文章

  • 【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/gwhnuhtx.html