React Umi 中实现缓存路由 KeepAlive

使用umi-plugin-keep-alive实现KeepAlive状态存储,
并且实现
进入下一个页面时缓存,后退页面时不缓存(卸载)
类似微信小程序的页面缓存机制

1、安装

npm install umi-plugin-keep-alive --save
// 或
yarn add umi-plugin-keep-alive

2、使用在 Layout中

import './index.less';
import { useModel, KeepAlive, history } from 'umi';
import { useEffect } from 'react';

export default function Layout(props: any) {
  // 需要缓存的页面路由
  const keepAliveRoutes = ['/channel', '/search'];
  return (
    <>
      {keepAliveRoutes.includes(props?.location?.pathname || '') ? (
        <KeepAlive
          name={history.location.search || history.location.pathname}
          when={() => {
            // 根据路由的前进和后退状态去判断页面是否需要缓存,前进时缓存,后退时不缓存(卸载)。 when中的代码是在页面离开(卸载)时触发的。
            return history.action != 'POP'; // true卸载时缓存,false卸载时不缓存
          }}
        >
          <div className="ddmc-main">{props.children}</div>
        </KeepAlive>
      ) : (
        <div className="ddmc-main">{props.children}</div>
      )}
    </>
  );
}

上面demo使用的版本:
“react”: “17.x”,
“react-dom”: “17.x”,
“react-helmet”: “^6.1.0”,
“umi”: “3.5.15”,
“umi-plugin-keep-alive”: “0.0.1-beta.35”,

3.上面如果不好用看这里

如果上面不好用,说明 KeepAlive 没有真正包裹一个完整的组件。
需要做的是将 props.children 拆出来当一个全局组件塞进去。


import './index.less';
import { useModel, KeepAlive, history } from 'umi';
import { useEffect } from 'react';

const Child = (props: any) => {
  return <div className="ddmc-main">{props.children}</div>;
};

export default function Layout(props: any) {
  // 需要缓存的页面路由
  const keepAliveRoutes = ['/channel', '/search'];
  return (
    <>
      {keepAliveRoutes.includes(props?.location?.pathname || '') ? (
        <KeepAlive
          name={history.location.search || history.location.pathname}
          id={history.location.search || history.location.pathname}
          when={() => {
            // 根据路由的前进和后退状态去判断页面是否需要缓存,前进时缓存,后退时不缓存(卸载)。 when中的代码是在页面离开(卸载)时触发的。
            return history.action != 'POP'; // true卸载时缓存,false卸载时不缓存
          }}
        >
          {/* 为了使 KeepAlive 全局生效,需要包一层组件 */}
           <Child children={props.children}></Child>
        </KeepAlive>
      ) : (
        <div className="ddmc-main">{props.children}</div>
      )}
    </>
  );
}

此功能是基于 react-activation,更详细的可参考
https://github.com/CJY0208/react-activation/blob/master/README_CN.md


码字不易,觉得有帮助的小伙伴点个赞支持下~


在这里插入图片描述

扫描上方二维码关注我的订阅号~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
React,可以使用一些技术来缓存之前页面的数据,以便在切换回该页面时可以快速加载和显示数据。以下是一些常见的缓存数据的方法: 1. 状态管理库:使用状态管理库(如Redux、MobX)来管理应用的数据状态。通过将数据存储在全局状态,可以在切换路由时保留数据。当再次返回到之前的页面时,可以直接从状态获取数据进行显示。 2. 组件内部缓存:在组件内部使用state或者实例变量来缓存数据。当切换路由时,组件被卸载,但是这些数据仍然存在于组件的状态或实例。再次访问该页面时,可以从组件的状态或实例获取数据进行展示。 3. 浏览器缓存:使用浏览器的缓存机制(如LocalStorage、SessionStorage、IndexedDB)将数据存储在本地。当切换路由时,可以先从缓存尝试获取数据,如果存在则使用缓存数据进行展示。注意,使用浏览器缓存时需要考虑数据的有效期和清理策略。 4. 路由级别缓存:一些路由库(如react-router)提供了缓存路由组件的功能。当切换回之前的页面时,路由库会自动从缓存获取组件并显示。这样可以避免重新渲染和加载数据,提高页面的渲染性能。 需要根据具体的应用场景和需求选择合适的缓存方法。在实现缓存时,还要考虑数据的更新和清理策略,以确保缓存的数据与实际数据保持同步。同时,也要注意缓存数据的大小和性能影响,避免过多的数据存储和加载导致性能问题。 希望对你有所帮助!如有任何疑问,请随时提出。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张兴华(MarsXH.Chang)

喜欢的可以请作者喝杯咖啡~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值