Skip to content

Instantly share code, notes, and snippets.

@akinolu52
Last active October 27, 2021 21:57
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 akinolu52/3d21d894e077226b2100e3119cf585e1 to your computer and use it in GitHub Desktop.
Save akinolu52/3d21d894e077226b2100e3119cf585e1 to your computer and use it in GitHub Desktop.
An implementation of react-native-async-storage
import AsyncStorage from '@react-native-async-storage/async-storage';
export const storeData = async (storageKey, value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem(`@${storageKey}`, jsonValue);
return true;
} catch (e) {
// saving error
return false;
}
}
export const getData = async storageKey => {
try {
const jsonValue = await AsyncStorage.getItem(`@${storageKey}`);
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
return false;
}
}
export const removeData = async storageKey => {
try {
await AsyncStorage.removeItem(`@${storageKey}`);
return true;
} catch (e) {
// remove error
return false;
}
}
export const clearData = async () => {
try {
await AsyncStorage.clear();
return true;
} catch (e) {
// clear error
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment