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

React——组件缓存 react-activation

1、安装依赖

npm i -S react-activation

2、包裹根组件

import { AliveScope } from "react-activation"<AliveScope><App />
</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>
}

4、路由缓存

<Routepath="/page1"render={props => (<KeepAlive name="/page1"><Item {...props} /></KeepAlive>)}
/>

5、KeepAlive属性

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

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

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

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

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

saveScrollPosition

Boolean自动保存滚动位置(默认true)
namestring缓存标识

6、手动控制缓存

使用 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() 获取所有缓存中的节点
http://www.lryc.cn/news/144807.html

相关文章:

  • EV代码签名证书是什么?
  • 融媒行业落地客户旅程编排,详解数字化用户运营实战
  • PDF制作成翻页电子书
  • 多线程
  • BingChat与ChatGPT比较,哪个聊天机器人能让你获益更多?
  • Qt读写ini配置文件(QSettings)、XML
  • JVM知识点(二)
  • 代码随想录算法训练营day44 | LeetCode 518. 零钱兑换 II 377. 组合总和 Ⅳ
  • Vue2向Vue3过度核心技术工程化开发和脚手架
  • Expected all tensors to be on the same device, but found at least two devices
  • Mysql备份命令Mysqldump导入、导出以及压缩成zip、gz格式
  • App卡帧与BlockCanary
  • bpmnjs Properties-panel拓展(ExtensionElements拓展篇)
  • 虚拟机的使用
  • CSS Flex布局
  • Virtual
  • 6、监测数据采集物联网应用开发步骤(5.2)
  • 解释 Git 的基本概念和使用方式
  • 不同ubuntu系统下的不同ros系统可以互相通讯吗
  • 数学建模-模型详解(2)
  • IT运维:使用数据分析平台监控DELL服务器
  • Spring Cloud Alibaba-Sentinel规则
  • go http-proxy
  • 用变压器实现德-英语言翻译【01/8】:嵌入层
  • 【vue3.0中ref与reactive的区别及使用】
  • 计算机竞赛 基于情感分析的网络舆情热点分析系统
  • C++ 动态分配内存|动态数组
  • React Diff算法原理
  • 查局域网所有占用IP
  • 【MySQL】引擎类型