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

vite+react 使用 react-activation 实现缓存页面

对应的版本


"react": "^18.2.0",
"react-activation": "^0.12.4",
"react-dom": "^18.2.0",
"react-router-dom": "^6.15.0",

react-activation

这是一个npm包,在react keep alive中用的人数应该是最多的包.
这是GitHub地址react-activation

安装

yarn add react-activation
# 或者
npm install react-activation
# 或者
pnpm install react-activation 

   现在 main.tsx  入口文件中 使用 

import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import App from "./App.tsx";
import { AliveScope } from "react-activation";const { VITE_PROJECT_BASE } = import.meta.env;ReactDOM.createRoot(document.getElementById("root")!).render(<BrowserRouter basename={VITE_PROJECT_BASE}><AliveScope>  //  添加这个 AliveScope<App /></AliveScope></BrowserRouter>
);

  route.ts 路由文件中的使用

import { Outlet } from "react-router-dom";
import { RoutesTypes } from "@/types/routesType";
import { TableOutlined } from "@ant-design/icons";
import KeepAlive from "react-activation";  // 添加这个 包裹对应的组件
// id  是 用作 唯一标识的  name 的 是 后期 调用  react-activation 对应方法,去除,和刷新组件// 房间
import RoomList from "@/pages/RoomManagement/RoomList/index.tsx";// 公播
import PublicBroadcastingList from "@/pages/PublicBroadcastingManagement/PublicBroadcastingList/index.tsx";
import DefaultPublicBroadcasting from "@/pages/PublicBroadcastingManagement/DefaultPublicBroadcasting/index.tsx";// 设备
import DeviceList from "@/pages/DeviceManagement/DeviceList/index.tsx";
import DeviceConfiguration from "@/pages/DeviceManagement/DeviceConfiguration/index.tsx";const routes: RoutesTypes[] = [{sort: 1,path: "/layout/sass-platform",element: <Outlet />,meta: {title: "测试平台",},children: [{path: "/layout/sass-platform/room-management",element: <Outlet />,meta: {title: "房间管理",icon: TableOutlined,},children: [{path: "/layout/sass-platform/room-management/room-list",element: (<KeepAliveid="/layout/sass-platform/room-management/room-list"name="/layout/sass-platform/room-management/room-list"><RoomList /></KeepAlive>),meta: {title: "房间列表",icon: TableOutlined,},},],},{path: "/layout/sass-platform/public-broadcasting-management",element: <Outlet />,meta: {title: "公播管理",icon: TableOutlined,},children: [{path: "/layout/sass-platform/public-broadcasting-management/public-broadcasting-list",element: (<KeepAliveid="/layout/sass-platform/public-broadcasting-management/public-broadcasting-list"name="/layout/sass-platform/public-broadcasting-management/public-broadcasting-list"><PublicBroadcastingList /></KeepAlive>),meta: {title: "公播列表",icon: TableOutlined,},},{path: "/layout/sass-platform/public-broadcasting-management/default-public-broadcasting",element: (<KeepAliveid="/layout/sass-platform/public-broadcasting-management/default-public-broadcasting"name="/layout/sass-platform/public-broadcasting-management/default-public-broadcasting"><DefaultPublicBroadcasting /></KeepAlive>),meta: {title: "默认公播",icon: TableOutlined,},},],},{path: "/layout/sass-platform/device-management",element: <Outlet />,meta: {title: "设备管理",icon: TableOutlined,},children: [{path: "/layout/sass-platform/device-management/device-list",element: (<KeepAliveid="/layout/sass-platform/device-management/device-list"name="/layout/sass-platform/device-management/device-list"><DeviceList /></KeepAlive>),meta: {title: "设备列表",icon: TableOutlined,},},{path: "/layout/sass-platform/device-management/device-configuration",element: (<KeepAliveid="/layout/sass-platform/device-management/device-configuration"name="/layout/sass-platform/device-management/device-configuration"><DeviceConfiguration /></KeepAlive>),meta: {title: "设备配置",icon: TableOutlined,},},],},],},
];export default routes;

缓存的路由的不能懒加载

最后 路由的显示

Outlet 放置位置

文档:https://github.com/CJY0208/react-activation/blob/master/README_CN.md

手动控制缓存

使用 withAliveScope 或 useAliveController 获取控制函数

import KeepAlive, { withAliveScope, useAliveController } from 'react-activation'//hook 函数式组件
function app(){const { drop, dropScope, clear, getCachingNodes } = useAliveController()
}//class 类组件
@withAliveScope
class App extends Component {render() {const { drop, dropScope, clear, getCachingNodes } = this.props}
}
  • drop(name) 卸载缓存,不包括嵌套的KeepAlive
  • dropScope(name) 卸载缓存,包括嵌套的所有KeepAlive
  • refresh(name) 刷新缓存状态,不包括嵌套的KeepAlive
  • refreshScope(name) 刷新缓存状态,包括嵌套的所有KeepAlive
  • clear() 清空所有缓存
  • getCachingNodes() 获取所有缓存中的节点

KeepAlive属性 

属性名类型备注
whenBoolean、Array、Function

Boolean (true-卸载时缓存 false-卸载时不缓存)

Array (第 1 位参数表示是否需要在卸载时缓存

第 2 位参数表示是否卸载 <KeepAlive> 的所有缓存内容,包括 <KeepAlive> 中嵌的 <KeepAlive>

// 例如:以下表示卸载时不缓存,并卸载掉嵌套的所有 `<KeepAlive>`
<KeepAlive when={[false, true]}>...<KeepAlive>...<KeepAlive>...</KeepAlive>...</KeepAlive>...
</KeepAlive>

Function (返回值为上述 Boolean 或 Array)

saveScrollPosition

Boolean自动保存滚动位置(默认true)
namestring缓存标识
http://www.lryc.cn/news/165263.html

相关文章:

  • 【android 蓝牙开发——蓝牙耳机】
  • Golang goroutine 进程、线程、并发、并行
  • 如何做到安全上网
  • 优维低代码实践:菜单
  • git merge 如何撤销
  • 解读package.json 中的功能
  • UMA 2 - Unity Multipurpose Avatar☀️四.UMA人物部位的默认颜色和自定义(共享)颜色
  • phpstorm配置php运行环境
  • 算法训练营day49|动态规划 part10:(LeetCode 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II)
  • Swagger 使用教程
  • 单例模式-饿汉模式、懒汉模式
  • UG\NX二次开发 复制3元素的double数组到另一个数组 UF_VEC3_copy
  • 骨传导耳机对人体有危险吗?会损害听力吗?
  • Spring Boot @Value读不到Nacos配置中心的值。(properties配置文件)
  • Rocky Linux怎么安装mysql
  • 轻量级软件FastGithub实现稳定访问github
  • 芯科蓝牙BG27开发笔记6-精简第一个程序
  • Android8.1 hal 加载wifi ko模块流程
  • Unity SteamVR 开发教程:SteamVR Input 输入系统(2.x 以上版本)
  • PyTorch中,卷积层、池化层、转置卷积层输出特征图形状计算公式总结
  • Git Cherry Pick命令
  • 算法:经典贪心算法--跳一跳[2]
  • Vue 和 React 前端框架的比较
  • 【Java】什么是过滤器链(FilterChain )?哪些场景可以使用过滤器链?
  • Vue-video-player下载失败(npm i 报错)
  • 数据在内存中的存储(1)
  • LINUX常用命令练习
  • 2022年全国研究生数学建模竞赛华为杯C题汽车制造涂装-总装缓存调序区调度优化问题求解全过程文档及程序
  • 文本直接生成3D游戏场景、功能,用ChatGPT方式开发游戏!
  • 2023年会展行业研究报告