@react-native-pure/async-storage-extra
v2.0.4
Published
AsyncStorage扩展
Downloads
2
Readme
async-storage-extra
AsyncStorage扩展,所有AsyncStorage中的数据都可以同步进行读写,同时数据也会被持久化.可以监听每个key的变化
Install
$ npm i @react-native-pure/async-storage-extra --save
Simple Usage
import Storage from "@react-native-pure/async-storage-extra
const User=new Storage("@user");
User.setItem("name","Mr");
User.setItem("age",10);
User.setItem("birthday",new Date());
User.setItem("other",{});
绑定Storage中的数据到Component
import * as React from "react"
import {View,Text} from "react-native"
import {storage} from "@react-native-pure/async-storage-extra
@storage(
User.connect(["name","age"],([name,age])=>({name,age}))
)
class User extends React.Component{
render(){
return (
<View>
<Text>Name : {this.props.name}</Text>
<Text>Age : {this.props.age}</Text>
</View>
);
}
}
Storage
默认情况下,在初始化Storage时会自动从AsyncStorage中恢复数据,数据恢复完成会触发onPreload
事件.
PS:当使用setItem,multiSet设置的数据和存在的数据一样时(DeepEqual),将不会触发任何事件和写操作.
constructor
(option:string|StorageOption={prefix:"@storage",preload:true})=>voidgetItem
(key:string)=>anymultiGet
(keys:Array)=>Array<[key,value]>search
(pattern:RegExp)=>Array<[key,value]>setItem
(key:string,value:any)=>voidmultiSet
(keyValuePairs:Array<[key,value]>)=>voidremoveItem
(key:string)=>voidmultiRemove
(keys:Array)=>voidclear
()=>voidgetAllKeys
()=>ArraygetKeys
(pattern:RegExp)=>ArrayaddListener
(key:string,callback:(value:any)=>void)=>Objectonce
(key:string,callback:(value:any)=>void)=>ObjectremoveAllListeners
(key:string)=>voidconnect
(keys:Array,mapStateToProps:(Array)=>Object)=>voidrestore
()=>void 从AsyncStorage中恢复数据release
()=>void 释放内存,Storage中的数据将不可用
StorageOption
prefix
stringpreload
booleanonPreload
(self:Storage)=>void