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

react-activation实现缓存,且部分页面刷新缓存,清除缓存

1.安装依赖

npm i -S react-activation

2.使用AliveScope 包裹根组件

import { AliveScope } from "react-activation"
<AliveScope><Router><Switch><Route exact path="/" render={() => <Redirect to="/login" push />} />        <Route path="/login" component={Login} /><Route path="/aaaa" component={App} /><Route path="/404" component={NotFound} /><Route component={NotFound} /></Switch></Router>
</AliveScope>

3.缓存组价或者路由

缓存组件

import { KeepAlive } from "react-activation"
export default () => {const [isShow, setIsShow] = useState(true)return <div><button onClick={() => setIsShow(!isShow)}>切换</button>{isShow &&<KeepAlive><Page3 /></KeepAlive>}<Page4 /></div>
}

缓存路由

import KeepAlive from 'react-activation'
//name属性,是为了刷新时候,判断路由
<KeepAlive name={this.props.path}><Route  path={path} exact={exact}  strict={strict}  render={(props)=>( <Component {...props} /> )} />
</KeepAlive>

4.keepAlive属性

属性名属性备注
whenBoolean、Array、FunctionBoolean (true-卸载时缓存 false-卸载时不缓存) Array (第 1 位参数表示是否需要在卸载时 第 2 位参数表示是否卸载 的所有缓存内容,包括 中嵌的 ) Function (返回值为上述 Boolean 或 Array)
saveScrollPositionBoolean自动保存滚动位置(默认true)
namestring缓存标识

5.在组件中手动刷新缓存,清除缓存

类组件

import { withAliveScope } from 'react-activation'class roleSetting extends Component {//组件内容//在路由跳转之前,将要跳转的二级页面缓存清空,解决从列表页跳转详情时,只有第一次掉接口,其他情况均不掉接口问题this.props.refreshScope('bbb')this.props.history.push({ pathname: "bbb", state: state});//清除所有缓存this.props.clear();
}
export default withAliveScope(roleSetting )

函数组件

import { useAliveController } from 'react-activation'
const TeamList = (props) => {const { refreshScope, clear } = useAliveController()//在路由跳转之前,将要跳转的二级页面缓存清空,解决从列表页跳转详情时,只有第一次掉接口,其他情况均不掉接口问题refreshScope('aaa')props.history.push({pathname: 'aaa',state: state,});//清除所有缓存,比如退出登录时候clear();
}
export default TeamList

drop(name) 卸载缓存,不包括嵌套的KeepAlive
dropScope(name) 卸载缓存,包括嵌套的所有KeepAlive
refresh(name) 刷新缓存状态,不包括嵌套的KeepAlive
refreshScope(name) 刷新缓存状态,包括嵌套的所有KeepAlive
clear() 清空所有缓存
getCachingNodes() 获取所有缓存中的节点

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

相关文章:

  • idea 中 tomcat 乱码问题修复
  • Modbus协议学习第七篇之libmodbus库API介绍(modbus_write_bits等)
  • 第九节HarmonyOS 常用基础组件13-TimePicker
  • 力扣刷题-55.跳跃游戏
  • Ruby安装演示教程
  • 前端使用vue-simple-uploader进行分片上传
  • Java 源代码中常见的数据类型
  • Web3行业研究逐步加强,“链上数据”缘何成为关注焦点?
  • 逸学区块链【solidity】真随机数
  • 【WPF.NET开发】优化性能:对象行为
  • uniapp中封装一个svg转base64的组件
  • QT播放gstreamer命令(三)---使用QMediaPlayer
  • Ubuntu22扩大分区
  • 数据结构篇-05:哈希表解决字母异位词分组
  • 添加了gateway之后远程调用失败
  • C#,哥伦布数(Golomb Number)的算法与源代码
  • JVM学习
  • Visual Studio 20XX中utf-8中文在控制台显示乱码
  • 拥抱个人成长与社会进步:自我认知与开放心态的相互影响
  • 【PostgreSQL内核学习(二十五) —— (DBMS存储空间管理)】
  • 2024年 复习 HTML5+CSS3+移动web 笔记 之CSS遍 第5天
  • SpringBoot使用Kafka详解含完整代码
  • 解决:java -jar 在cmd中运行 程序卡顿,卡死的 问题。BIO和NIO案例保存
  • LeetCode第824题 - 山羊拉丁文
  • [Python] 什么是逻辑回归模型?使用scikit-learn中的LogisticRegression来解决乳腺癌数据集上的二分类问题
  • 那些不输于乙游男主人设的国漫男主
  • Apache Doris 整合 FLINK CDC + Iceberg 构建实时湖仓一体的联邦查询
  • 关于华为应用市场上架,申请权限未告知目的被驳回问题的简单处理方式
  • 【ElasticSearch】概述
  • 十进制转十六进制 C/C++蓝桥杯基础试题BASIC-10