yarn add @react-native-async-storage/async-storage // install by yarn
npm i @react-native-async-storage/async-storage // install by npm
// import
import AsyncStorage from '@react-native-async-storage/async-storage';
await AsyncStorage.setItem('@storage_Key', "value") // for store item
await AsyncStorage.removeItem('@storage_Key', "value") // for remove item
getMyObject = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@key')
return jsonValue != null ? JSON.parse(jsonValue) : null
} catch(e) {
// read error
}
console.log('Done.')
}
setObjectValue = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('key', jsonValue)
} catch(e) {
// save error
}
console.log('Done.')
}
getMyStringValue = async () => {
try {
return await AsyncStorage.getItem('@key')
} catch(e) {
// read error
}
console.log('Done.')
}
setStringValue = async (value) => {
try {
await AsyncStorage.setItem('key', value)
} catch(e) {
// save error
}
console.log('Done.')
}
static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => void): Promise
static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void): Promise