React Native AsyncStorage — Persisting data to Local or Device Storage

  • AsyncStorage is a key-value storage system that is global to the app.
  • it is unencrypted and asynchronous.
  • It should be used instead of LocalStorage.
yarn add @react-native-community/async-storage

The Simple Code

import AsyncStorage from '@react-native-community/async-storage';const WISHLIST = '@WISHLIST'; //simple key titleconst storeWishlist = async (email, data) => {
const compoundKey = email + WISHLIST;
try {
await AsyncStorage.setItem(compoundKey, JSON.stringify(data));
} catch (err) {
console.log(err);
}
};
const retrieveWishlist = async (email) => {
const compoundKey = email + WISHLIST;
let result = null;

if (!email) {
return false;
}
try {
result = await AsyncStorage.getItem(compoundKey);
if (result !== null) {
return JSON.parse(result);
}
return false;
} catch (err) {
console.log(err);
return false;
}
};
const deleteWishlist = (email) => {
const compoundKey = email + WISHLIST;
AsyncStorage.removeItem(compoundKey);
};

The methods:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store