当前位置:   article > 正文

React中使用useActive

React中使用useActive

1.引入

import { useActivate } from "react-activation";
  • 1

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 | ReactNodeArray
  name?: string
  id?: string
  cacheKey?: string
  when?: boolean | Array<boolean> | (() => boolean | Array<boolean>)
  saveScrollPosition?: boolean | string
  autoFreeze?: boolean
  [key: string]: any
}

export declare class KeepAlive extends Component<KeepAliveProps> {}
export default KeepAlive

export declare class AliveScope extends Component<{
  children: ReactNode | ReactNodeArray
}> {}

export declare class NodeKey extends Component<{
  prefix?: string
  onHandleNode?: (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[]): void

export function useActivate(effect: () => void): void
export function useUnactivate(effect: () => void): void

export interface CachingNode {
  createTime: number
  updateTime: number
  name?: string
  id: 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(): AliveController

export declare function withActivation<C extends ComponentType<GetProps<C>>>(component: C): C
export declare function withAliveScope<C extends ComponentType<GetProps<C>>>(component: C): C

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

在这里插入图片描述

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

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

  • 缓存控制函数

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

  • 使用: 当不同页签下面使用同一个列表数据,当一个页签下的数据改变的时候,另一个使用数据相等的页面在激活页签的时候自动改变,需要使用 useActive() 来包裹,就可以实现实时更新的效果。
useActive(() => {
	getList()  // 获取数据
})
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/186823
推荐阅读
相关标签
  

闽ICP备14008679号