grooot
grooot

Reputation: 466

Local Storage - React Native

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

Upvotes: 5

Views: 6813

Answers (4)

Navin Kumar
Navin Kumar

Reputation: 4027

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

Upvotes: 1

Kerem atam
Kerem atam

Reputation: 2777

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

Install it and import it from lib:

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

Upvotes: 2

Guruparan Giritharan
Guruparan Giritharan

Reputation: 16334

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

Upvotes: 3

Mahdi N
Mahdi N

Reputation: 2178

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';

Upvotes: 0

Related Questions