asyncstorage react native code example
Example 1: react native async storage
yarn add @react-native-async-storage/async-storage
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeData = async (value) => {
try {
await AsyncStorage.setItem('@storage_Key', value)
} catch (e) {
}
}
const getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key')
if(value !== null) {
}
} catch(e) {
}
}
Example 2: how to install asyncstorage in react native
npm i @react-native-community/async-storage
Example 3: react native typescript
npx react-native init MyApp --template react-native-template-typescript
Example 4: react native storage
npm install react-native-storage
npm install @react-native-community/async-storage
Example 5: asyncstorage.getallkeys
importData = async () => {
try {
const keys = await AsyncStorage.getAllKeys();
const result = await AsyncStorage.multiGet(keys);
return result.map(req => JSON.parse(req)).forEach(console.log);
} catch (error) {
console.error(error)
}
}
Example 6: fetch data from asyncstorage react native
let user = await AsyncStorage. getItem('user');
let parsed = JSON. parse(user);