A local storage wrapper for both react-native(AsyncStorage) and browser(localStorage).
Features
- Promise based for async load.
- Size controlling.
- Auto expiring.
- Remote data auto syncing.
- Get a batch data in one query.
Install
in react-native environment require react-native version >= 0.13
npm install react-hybrid-storage --save OR yarn add react-hybrid-storage
Usage
Import
;
Do not use require('react-hybrid-storage')
, which would cause error in react-native version >= 0.16.
Init
;; // Don't import if the target is web const storage = size : 1000 // maximum capacity, default 1000 storageBackend : AsyncStorage // Use AsyncStorage for RN, or window.localStorage for web. defaultExpires : 1000 * 3600 * 24 // expire time, default 1 day(1000 * 3600 * 24 milliseconds). enableCache : true // cache data in the memory. default is true. sync : {} // I suggest you have one(and only one) storage instance in global scope. // for webwindowstorage = storage; // for react-nativeglobalstorage = storage;
Save
- key : should be an unique string value and are permanently stored unless you remove, do not use underscore("_") in key!
- data : any valid JSON object to save.
- expires : expire time in milliseconds, if set to null, then it will never expire.
storage;
Load
- key : a string value (key) that we want to load.
- autoSync : if data not found or expired, then invoke the corresponding sync method.
- syncInBackground : if data expired, return the outdated data first while invoke the sync method.
- syncParams : you can pass extra params to sync method.
try const token refresh = await storage; console; catch error // Do something
Credits
This library is a rewrite of react-native-storage originally created by sunnylqm. We really appreciate the effort he did solving the issue we face using localstorage and async storage in multiplatform enviorments.
License
MIT