1.0.10 • Published 3 years ago

@tgu/usestorage v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

@tgu/useStorage

本地数据缓存

import { useStorageState } from '@tgu/usestorage'

Example

import { useSessionStorageState } from '@tgu/usestorage'
const [userInfo, setUserInfo] = useSessionStorageState<any>('userInfo', {})
setUserInfo({ userId: 1 })
consle.log('userInfo: ', userInfo)

useStorage

useStorage是前端缓存库 - 可以使用localstorage/sessionstorage,也可以使用自定义的cacheStorage

Usage

const state, updateStateFn, deleteStateFn = useStorageState(cacheStorage, key, defaultValue)

Params

PropertyDescriptionTypeDefaultRequired
cacheStorage缓存类库Storagenulltrue
key缓存字段名stringnulltrue
defaultValue默认参数anynullfalse

Result

PropertyDescriptionTypeRequired
state状态anytrue
updateState更新状态的方法functionfalse
deleteState删除状态的方法functionfalse

Storage

自己封装的cacheStorage必须要带有一下方法

APIDescriptionParams
getItem获取缓存方法key
setItem设置缓存方法key, value
removeItem移除缓存方法key
clear清除所有缓存方法null
updateLength更新缓存长度null
key根据设置缓存的索引获取缓存keyindex

altStorageState

altStorageState 的使用方法与 useStorageState 完全一致,唯一的区别在于 useStorageState是以react的hooks方式使用,altStorageState是在非hooks版本中使用

其它storage

以useLocalStorageState为例

Usage

const [state, updateStateFn, deleteStateFn] = useLocalStorageState(key, defaultValue)

Params

PropertyDescriptionTypeDefaultRequired
key缓存字段名stringnulltrue
defaultValue默认参数anynullfalse

Result

PropertyDescriptionTypeRequired
state状态anytrue
updateState更新状态的方法functionfalse
deleteState删除状态的方法functionfalse

其它

还支持的API用法与useLocalStorageState完全一致。唯二区别:

  1. alt/use 函数用法/hooks用法
  2. use/altstate, StorageType表示对应的缓存库
APIDescription
useLocalStorageStatehooks使用localstorage缓存
altLocalStorageState非hooks使用localstorage缓存
useSessionStorageStatehooks使用sessionstorage缓存
altSessionStorageState非hooks使用sessionstorage缓存
usecacheStorageStatehooks使用自定义的map缓存
altCacheStorageState非hooks使用自定义的map缓存
1.0.10

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago