Skip to content

Instantly share code, notes, and snippets.

@danrigsby
Created November 25, 2015 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danrigsby/79579d03cffae760f43d to your computer and use it in GitHub Desktop.
Save danrigsby/79579d03cffae760f43d 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;
@amun1303
Copy link

amun1303 commented Mar 9, 2018

Hello
I tried and I stuck on getItem from LocalStorage
This is my code:
LocalStorage.save('text', 'Hello');
console.log(LocalStorage.get('text'));

and this is the result
image

So how to get exactly the String value "Hello"

@geminiyellow
Copy link

geminiyellow commented Jun 18, 2018

yep, same, i use async/await

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment