Last active
October 27, 2021 21:57
-
-
Save akinolu52/3d21d894e077226b2100e3119cf585e1 to your computer and use it in GitHub Desktop.
An implementation of react-native-async-storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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