Miss any of our Open RFC calls?Watch the recordings here! »

@react-native-pure/async-storage-extra

2.0.4 • Public • Published

async-storage-extra

AsyncStorage扩展,所有AsyncStorage中的数据都可以同步进行读写,同时数据也会被持久化.可以监听每个key的变化

Build Status Coverage Status

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})=>void
  • getItem (key:string)=>any
  • multiGet (keys:Array)=>Array<[key,value]>
  • search (pattern:RegExp)=>Array<[key,value]>
  • setItem (key:string,value:any)=>void
  • multiSet (keyValuePairs:Array<[key,value]>)=>void
  • removeItem (key:string)=>void
  • multiRemove (keys:Array)=>void
  • clear ()=>void
  • getAllKeys ()=>Array
  • getKeys (pattern:RegExp)=>Array
  • addListener (key:string,callback:(value:any)=>void)=>Object
  • once (key:string,callback:(value:any)=>void)=>Object
  • removeAllListeners (key:string)=>void
  • connect (keys:Array,mapStateToProps:(Array)=>Object)=>void
  • restore ()=>void 从AsyncStorage中恢复数据
  • release ()=>void 释放内存,Storage中的数据将不可用

StorageOption

  • prefix string
  • preload boolean
  • onPreload (self:Storage)=>void

Install

npm i @react-native-pure/async-storage-extra

DownloadsWeekly Downloads

4

Version

2.0.4

License

MIT

Unpacked Size

18.1 kB

Total Files

9

Last publish

Collaborators

  • avatar
  • avatar