当前位置: 首页 > news >正文

性能优化-react路由懒加载和组件懒加载

背景

随着项目越来越大,打包后的包体积也越来越大,严重影响了首屏加载速度,需要对路由和组件做懒加载处理

主要用到了react中的lazy和Suspense。

废话不多说,直接上干货

路由懒加载

核心代码

import React, { lazy, Suspense } from "react";
const loading = () => <h3>loading....</h3>;
const Caidan1 = lazy(() => import("@/pages/mud1/caidan1"));const meunRoutes = [{name: "模块1",path: "/m1",icon: <AppstoreOutlined />,children: [{name: "gltf模型",path: "/m1/caidan12",icon: <AppstoreOutlined />,element: (<Suspense fallback={loading()}><Caidan1 /></Suspense>),},// 。。。。

配合路由表的完整例子

// 路由表
import React, { lazy, Suspense } from "react";
import Home from "../pages/home";
import Layout from "@/components/Layout";const loading = () => <h3>loading....</h3>;const Caidan1 = lazy(() => import("@/pages/mud1/caidan1"));
const Caidan2 = lazy(() => import("@/pages/mud1/caidan2"));
// 404页面
const NotFound = () => <h1>**** 404 ****</h1>;const meunRoutes = [{name: "模块1",path: "/m1",icon: <AppstoreOutlined />,children: [{name: "gltf模型",path: "/m1/caidan12",icon: <AppstoreOutlined />,element: (<Suspense fallback={loading()}><Caidan1 /></Suspense>),},{name: "模型动画",path: "/m1/caidan13",icon: <AppstoreOutlined />,element: (<Suspense fallback={loading()}><Caidan2 /></Suspense>),},],},
];// 配置路由表
const routes = [{path: "/",element: <Navigate to="/home" />,},{path: "/home",element: <Home />,},{path: "/",element: <Layout />,children: handleMenuRoutes(meunRoutes),},{ path: "*", element: <NotFound /> },
];// 处理menu routes
function handleMenuRoutes(arr) {let res = [];arr.forEach((item) => {if (item.children && item.children.length > 0) {item.children.forEach((yitem) => {let obj = {path: yitem.path,element: yitem.element,};res.push(obj);});}});return res;
}const AppRouter = () => useRoutes([...routes]);
export { AppRouter, meunRoutes };

组件懒加载

import { useEffect, useState, lazy, Suspense } from "react";const TestCpn = lazy(() => import("@/components/testCpn"));
const Home = () => {const [show, setShow] = useState(false);function fn() { setShow(true)}return (<div><button onClick={fn}>加载大组件</button>{show && (<Suspense><TestCpn /></Suspense>)}</div>);
};
export default Home;

效果
组件加载前
在这里插入图片描述
组件懒加载后
在这里插入图片描述
这样就会大大加快首屏加载速度

http://www.lryc.cn/news/109393.html

相关文章:

  • 静态网页加速器:优化性能和交付速度的 Node.js 最佳实践
  • Spring 非自定义Bean注解
  • 微信小程序:点击按钮实现数据加载(带模糊查询)
  • 2023-2029年中国烘焙工坊市场经营管理风险与未来竞争优势分析报告
  • 用Rust实现23种设计模式之适配器
  • 替换开源LDAP,西井科技用宁盾目录统一身份,为业务敏捷提供支撑
  • 靶形数独
  • C语言阶段性测试题
  • java工厂设计模式
  • idea运行web老项目
  • JS进阶-Day3
  • springboot后端用WebSocket每秒向前端传递数据,python接收数据
  • 记录uniapp 滚动后溢出显示空白的办法
  • 设计原则学习之里氏替换原则
  • 排序进行曲-v4.0
  • Flink 系列四 Flink 运行时架构
  • 14-3_Qt 5.9 C++开发指南_QUdpSocket实现 UDP 通信_UDP 单播和广播
  • 【知识图谱】图数据库Neo4jDesktop的安装图文详解(小白适用)
  • kafka中幂等性producer和事务性producer
  • 静态路由 (华为设备)
  • Django学习笔记-默认的用户认证系统(auth)
  • [SQL挖掘机] - 存储过程
  • MySQL8.0.32详细安装教程(奶妈级手把手教你安装)
  • glut太阳系源码修改和对cpu占用观察
  • 掌握NLTK:Python自然语言处理库中级教程
  • Go语言的崛起:探究越来越多公司选择Go语言的原因和优势
  • MongoDB 6.0.8 安装配置
  • 无涯教程-Lua - nested语句函数
  • 如何使用vue ui创建一个项目?
  • STM32——LED内容补充(寄存器点灯及反转的原理)