react native async storage example

Example 1: react native async storage

yarn add @react-native-async-storage/async-storage

import AsyncStorage from '@react-native-async-storage/async-storage';
// store item
const storeData = async (value) => {		
  try {
    await AsyncStorage.setItem('@storage_Key', value)
  } catch (e) {
    // saving error
  }
}
// get item
const getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

Example 2: fetch data from asyncstorage react native

let user = await AsyncStorage. getItem('user');
let parsed = JSON. parse(user);