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

React中使用useActive

1.引入

import { useActivate } from "react-activation";

2.React Activation

React中使用react-activation,其实就是类似于Vue中的keep-alive,实现数据的缓存;

源码:

import { ReactNode, ReactNodeArray, Context, Component, ComponentType } from 'react'export declare type GetProps<C> = C extends ComponentType<infer P> ? P : never;export interface KeepAliveProps {children: ReactNode | ReactNodeArrayname?: stringid?: stringcacheKey?: stringwhen?: boolean | Array<boolean> | (() => boolean | Array<boolean>)saveScrollPosition?: boolean | stringautoFreeze?: boolean[key: string]: any
}export declare class KeepAlive extends Component<KeepAliveProps> {}
export default KeepAliveexport declare class AliveScope extends Component<{children: ReactNode | ReactNodeArray
}> {}export declare class NodeKey extends Component<{prefix?: stringonHandleNode?: (node: any, mark?: string) => string | undefined | null
}> {}export function fixContext(context: Context<any>): void
export function createContext<T>(defaultValue: T,calculateChangedBits?: (prev: T, next: T) => number
): Context<T>
// type ContextFixEntry = [host: any, ...methods: any[]]
export function autoFixContext(...configs: any[]): voidexport function useActivate(effect: () => void): void
export function useUnactivate(effect: () => void): voidexport interface CachingNode {createTime: numberupdateTime: numbername?: stringid: string[key: string]: any
}
export interface AliveController {drop: (name: string | RegExp) => Promise<boolean>dropScope: (name: string | RegExp) => Promise<boolean>refresh: (name: string | RegExp) => Promise<boolean>refreshScope: (name: string | RegExp) => Promise<boolean>clear: () => Promise<boolean>getCachingNodes: () => Array<CachingNode>
}
export function useAliveController(): AliveControllerexport declare function withActivation<C extends ComponentType<GetProps<C>>>(component: C): C
export declare function withAliveScope<C extends ComponentType<GetProps<C>>>(component: C): C

在这里插入图片描述

  • 生命周期函数: 在激活和离开时触发

import { useActivate, useUnactivate, withActivation } from 'react-activation'

  • 缓存控制函数

import { withAliveScope, useAliveController } from 'react-activation'
在这里插入图片描述

  • 使用: 当不同页签下面使用同一个列表数据,当一个页签下的数据改变的时候,另一个使用数据相等的页面在激活页签的时候自动改变,需要使用 useActive() 来包裹,就可以实现实时更新的效果。
useActive(() => {getList()  // 获取数据
})
http://www.lryc.cn/news/308006.html

相关文章:

  • ElasticSearch安装和kibana控制台安装
  • VSCode安装与使用详细教程
  • 土壤墒情监测站的工作原理
  • Flutter 多标签页显示 有关TabController需要知道的知识
  • 【Elasticsearch专栏 16】深入探索:Elasticsearch的Master选举机制及其影响因素分析
  • Leetcode : 215. 数组中的第 K 个最大元素
  • node express实现Excel文档转json文件
  • 【算法分析与设计】最大二叉树
  • 面试问答总结之并发编程
  • 红外测温仪芯片方案开发设计
  • 五、数组——Java基础篇
  • 如何用golang写一个自己的后端框架
  • linux 如何给服务器批量做免密,如何批量挂在磁盘
  • Android Activity的生命周期详解
  • python学习笔记-内置类型
  • 校园微社区微信小程序源码/二手交易/兼职交友微信小程序源码
  • 如何在 Angular 中使用 NgTemplateOutlet 创建可重用组件
  • 改进的yolo交通标志tt100k数据集目标检测(代码+原理+毕设可用)
  • nginx 日志,压缩,https功能介绍
  • 代码随想录三刷day17
  • postcss-px-to-viewport include属性
  • C++设计模式——抽象工厂模式
  • Windows安装VNC连接工具并结合cpolar实现远程内网Ubuntu系统桌面
  • Vue3 Hooks函数使用及封装思想
  • YOLOv8改进涨点,添加GSConv+Slim Neck,有效提升目标检测效果,代码改进(超详细)
  • 华为s5720s-28p-power-li-ac堆叠配置
  • c# aes加密解密私钥公钥通钥
  • 上拉电阻与下拉电阻、电容的作用
  • 《Spring Security 简易速速上手小册》第1章 Spring Security 概述(2024 最新版)
  • vue页面菜单权限问题解决