我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

  • 框架

    • react

      • 说说对React的理解,有哪些特性
      • 说说对React高阶组件的理解
      • 谈谈React中fiber的理解
      • 说说对React事件机制的理解
      • 说说对React中受控组件和非受控组件的理解及应用场景
      • 说说对redux工作流程的理解
      • 说说对redux中间件的理解
      • 说说对state和props的理解,有什么区别
      • 说说函数组件和类组件的理解和区别
      • 说说真实DOM和VDOM的区别,优缺点
      • 说说React.memo&useMemo&useCallback区别
      • 说说React路由有几种模式以及实现原理
      • 说说React生命周期有哪些不同阶段?每个阶段对应的方法
      • 说说React事件绑定有哪些
      • 说说React中的setState执行机制
      • 说说React中refs的理解
      • React构建组件有哪些方式及区别
      • React引入css的方式有哪几种
      • 谈谈React中key的作用
      • React中如何性能优化
        • 是什么
        • 怎么用
          • 常规手段
        • 原理
        • FAQ
      • React中hooks能取代redux吗
      • React组件通信方式有哪些
  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 框架
  • react
fangdown
2021-08-01
目录

React中如何性能优化

# 是什么

通过一些手段减少不必要的计算及render次数

# 怎么用

  1. PureComponent/ShouldComponentUpdate
  • PureComponent 会对 props 与 state 做浅比较,所以一定要保证 props 与 state 中的数据是 immutable 的。
  • 如果你的数据不是 immutable 的,或许你可以自己手动通过 ShouldComponentUpdate 来进行深比较。当然深比较的性能一般都不好,不到万不得已,最好不要这样搞。
  1. React.memo
  2. useMemo
  3. useCallback

# 常规手段

  1. 避免内联函数
  2. 使用<>空标签
  3. 使用immutable数据 Immutable通过is方法则可以完成对比,而无需像一样通过深度比较的方式比较
  4. 使用懒加载
  5. 事件绑定方式优化
  6. 服务端渲染

# 原理

// 内联函数每次都重新创建
import React from "react";
export default class InlineFunctionComponent extends React.Component {
  render() {
    return (
      <div>
        <h1>Welcome Guest</h1>
        <input type="button" onClick={(e) => { this.setState({inputValue: e.target.value}) }} value="Click For Inline Function" />
      </div>
    )
  }
}
// 空标签
export default class NestedRoutingComponent extends React.Component {
    render() {
        return (
            <>
                <h1>This is the Header Component</h1>
                <h2>Welcome To Demo Page</h2>
            </>
        )
    }
}
// 懒加载
const johanComponent = React.lazy(() => import(/* webpackChunkName: "johanComponent" */ './myAwesome.component'));
 
export const johanAsyncComponent = props => (
  <React.Suspense fallback={<Spinner />}>
    <johanComponent {...props} />
  </React.Suspense>
);

import { renderToString } from "react-dom/server";
import MyPage from "./MyPage";
app.get("/", (req, res) => {
  res.write("<!DOCTYPE html><html><head><title>My Page</title></head><body>");
  res.write("<div id='content'>");  
  res.write(renderToString(<MyPage/>));
  res.write("</div></body></html>");
  res.end();
});

import ReactDOM from 'react-dom';
import MyPage from "./MyPage";
ReactDOM.render(<MyPage />, document.getElementById('app'));

# FAQ

#react
上次更新: 2021/12/19, 18:05:42
谈谈React中key的作用
React中hooks能取代redux吗

← 谈谈React中key的作用 React中hooks能取代redux吗→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式