Skip to content

Instantly share code, notes, and snippets.

@dani-z
Forked from danrigsby/LocalStorage.js
Created January 18, 2018 19:17
Show Gist options
  • Save dani-z/b211d6ed7f661dd3b35844d208218c49 to your computer and use it in GitHub Desktop.
Save dani-z/b211d6ed7f661dd3b35844d208218c49 to your computer and use it in GitHub Desktop.
React Native Local Storage Wrapper
import React from 'react-native';
var {
AsyncStorage
} = React;
var LocalStorage = {
get: function (key) {
return AsyncStorage.getItem(key).then(function(value) {
return JSON.parse(value);
});
},
save: function (key, value) {
return AsyncStorage.setItem(key, JSON.stringify(value));
},
update: function (key, value) {
return LocalStorage.get(key).then((item) => {
// if current value is a string, then overwrite; else merge objects
let v = (typeof value === 'string') ? value : Object.assign({}, item, value);
return AsyncStorage.setItem(key, JSON.stringify(v));
});
},
delete: function (key) {
return AsyncStorage.removeItem(key);
}
};
export default LocalStorage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment