5

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??

grooot
  • 446
  • 2
  • 10
  • 31
  • Does this answer your question? [How to store value in LocalStorage in React Native](https://stackoverflow.com/questions/47509558/how-to-store-value-in-localstorage-in-react-native) – Jony Oct 20 '22 at 18:12

4 Answers4

3

Async storage is not deprecated its moved to a separate package which you can install and use

https://github.com/react-native-community/async-storage/

Or for tokens you can use react-native-keychain which is a way secure package you can check it here. https://github.com/oblador/react-native-keychain

Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50
  • Haven't worked on `react-native`, but is there any particular reason why they don't use `localStorage`? – ABGR May 31 '20 at 11:49
  • from what i know, RN uses Async storage from the beginning as it designed for mobiles and it handles the storage according the platform – Guruparan Giritharan May 31 '20 at 11:53
  • @Guru https://reactnative.dev/docs/security : according to the React Native docs, Async storage is not for Token storage. As you mentioned ios can use Keychain and android can use shared preferences. Never used it before!!! some insight on Keychain and shared preferences pleasee??? is this the syntax "Keychain.setGenericPassword('token', token)" to store the Token in keychain????? – grooot May 31 '20 at 14:21
  • 1
    You can get an idea from this blog https://medium.com/react-native-training/securing-your-react-native-app-using-keychain-6de10850d203 as you can see in the docs you shared this library is recommended – Guruparan Giritharan May 31 '20 at 14:33
2

It is moved to @react-native-community/async-storage

Install it and import it from lib:

import AsyncStorage from '@react-native-community/async-storage';
Kerem atam
  • 2,387
  • 22
  • 34
1

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

Navin Kumar
  • 3,393
  • 3
  • 21
  • 46
0

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';
Mahdi N
  • 2,128
  • 3
  • 17
  • 38
  • Haven't worked on react-native, but is there any particular reason why they don't use localStorage? – ABGR May 31 '20 at 11:49
  • 1
    LocalStorage stores data in web browser, since react native allows to build mobile apps we cannot use localStorage. Async Storage is used to natively store key-value globally to the app – Mahdi N May 31 '20 at 11:58