Local Storage - React Native

Viewed 4890

As Async Storage is deprecated what other way is there to store a variable locally??

I have a React Native ios App with a Notification Centre.
Each time the user enters the Notification Centre a Cognito access Token was generated. To avoid excess of token generation the Tokens were saved through Async storage and their expiry was checked.
Now is there some other local storage in React Native that i can use??

4 Answers

AsyncStorage is deprecated so use below package

Get library With Yarn:

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

Once done, import the header

import AsyncStorage from '@react-native-async-storage/async-storage';

For store a value

const storeData = async (value) => {
  try {
    await AsyncStorage.setItem('@storage_Key', value)
  } catch (e) {
    // saving error
  }
}

Get a Value

const getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

For more: offical link

Async storage from react-native library is deprecated, they split it from react-native core into a community library.

You can always use Async Storage from this library

Just follow installation steps from docs. And you can import AsyncStorage like this

import AsyncStorage from '@react-native-community/async-storage';
Related