Help with AsyncStorage

Please provide the following:

  1. SDK Version: 36.0.0
  2. Platforms(Android/iOS/web/all): Android

Hello! I am trying to save user’s authentication token in localStorage.
In React native doc, AsyncStorage is deprecated and recommend using react-native-community/ async-storage

So I just installed V2 of that package. then I created localDeviceStorage.js and code looks like this.

  1 import AsyncStorageFactory from '@react-native-community/async-storage';
  2 import LegacyStorage from '@react-native-community/async-storage-backend-legacy';
  3 
  4 
  5 const legacyStorage = new LegacyStorage();
  6 export const storage = AsyncStorageFactory.create(legacyStorage);                                                                       
~                                                                                                                                           
~                                                                              

then tried to save storage.set("userToken", token);
But I got an error while building app.

Error:[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

In document of react-native-community/async-storage says

If you have an Expo app, AsyncStorage will not work unless you eject the app. After ejecting the app, you may have to manually link depending on whether you chose to use Expo SDK.

Do I need to eject the app for using new async-storage? or is there any workaround?
What is the difference between managed and bare workflow? Can I use expo sdk(like AppAuth for logging in using Facebook) in bare workflow?
I need your help!

No need to use the new AsyncStorage for now. It’s deprecated from react native core but still available. There was a post on expos canny that they will provide a fork or use the community package once AsyncStorage is deleted. No need to eject for this.

2 Likes

In addition to what @hirbod said, you could also use SecureStore instead. Rather don’t eject just to use the new AsyncStorage module.

If you do really want to use the new AsyncStorage you will need to eject.
Check the Workflows, Managed Workflow and Bare Workflow docs.

The docs for the different modules normally say whether the module works in the Bare Workflow etc. e.g. the Appauth Installation section says:

For managed apps, you’ll need to run expo install expo-app-auth . To use it in a bare React Native app, follow its installation instructions.

i.e. no reason it should not work.

But e.g. Expo’s Notifications module is not currently available in the Bare workflow:

This API is pre-installed in managed apps. It is not available for bare React Native apps, although there are some comparable libraries that you may use instead.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.