DekGenius.com
JAVASCRIPT
asyncstorage react native
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
how to install asyncstorage in react native
npm i @react-native-community/async-storage
react native asyncstorage getItem example
getMyObject = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@key')
return jsonValue != null ? JSON.parse(jsonValue) : null
} catch(e) {
// read error
}
console.log('Done.')
}
async storage react native
import AsyncStorage from '@react-native-async-storage/async-storage';
// store item
export const storeData = async (key, value) => {
try {
await AsyncStorage.setItem(key, value);
return {
success: true
}
} catch (error) {
return {
success: false,
error
}
}
}
// get item
export const getData = async (key) => {
try {
const value = await AsyncStorage.getItem(key)
return {
success: true,
value
}
} catch(error) {
return {
success: false,
error
}
}
}
export default {
getData,
storeData
}
react native asyncstorage setItem example
setObjectValue = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('key', jsonValue)
} catch(e) {
// save error
}
console.log('Done.')
}
react native asyncstorage getItem example
getMyStringValue = async () => {
try {
return await AsyncStorage.getItem('@key')
} catch(e) {
// read error
}
console.log('Done.')
}
react native asyncstorage setItem example
setStringValue = async (value) => {
try {
await AsyncStorage.setItem('key', value)
} catch(e) {
// save error
}
console.log('Done.')
}
react native asyncstorage getItem
static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => void): Promise
react native asyncstorage setItem
static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void): Promise
© 2022 Copyright:
DekGenius.com