Skip to content

Instantly share code, notes, and snippets.

@kikoseijo
Created February 1, 2018 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikoseijo/edc00af2dc7cf67b5d50802ba3bc292f to your computer and use it in GitHub Desktop.
Save kikoseijo/edc00af2dc7cf67b5d50802ba3bc292f to your computer and use it in GitHub Desktop.
import { AsyncStorage } from 'react-native';
// retrieve
export const get = async key => await AsyncStorage.getItem(key);
// store
export const set = async (key, value) => {
await AsyncStorage.setItem(key, value);
};
export const setObj = async (key, value) => {
await AsyncStorage.setItem(key, JSON.stringify(value));
};
// store multiple keys
export const storeCredentials = async (token, permissions) => {
await AsyncStorage.multiSet([
['token', token],
['permissions', JSON.stringify(permissions)]
]);
};
// clear key
export const remove = async key => await AsyncStorage.removeItem(key);
// clear multiple keys
export const clearCredentials = async () =>
await AsyncStorage.multiRemove(['token', 'permissions']);
export const printAllKeys = async () => {
await AsyncStorage.getAllKeys((err, keys) => {
AsyncStorage.multiGet(keys, (err, stores) => {
stores.map((result, i, store) => {
// get at each store's key/value so you can work with it
let key = store[i][0];
let value = store[i][1];
console.log('[key, value]', key, value);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment