5

I just started a new Managed Expo React Native project (Expo SDK 35, React Native 0.5.9) using [email protected] but even a fresh expo project with redux/redux-persist is currently throwing the error below.


Using AsyncStorage from react-native,

we get the error:

redux-persist: config.storage is required. Try using one of the provided storage engines import storage from 'redux-persist/lib/storage'


Using AsyncStorage from @react-native-community/async-storage,

we get the error:

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


Using storage from redux-persist/lib/storage,

we get the error:

console.error: "redux-persist failed to create sync storage. falling back to noop storage."


Question: How do we solve this problem without ejecting? Thanks!


redux-persist Code

Note: Previous attempts have been commented out:

// Chose 1 of the 3 storages
// import { AsyncStorage } from "react-native";
// import AsyncStorage from '@react-native-community/async-storage';
import storage from 'redux-persist/lib/storage'

import { createMigrate, persistStore, persistReducer } from "redux-persist";
import reducer from "../reducers";

const persistConfig = {
    key: 'root',
    version: 0,
    storage,    // 'redux-persist/lib/storage'
}

// const persistConfig = {
//   key: 'root',
//   version: 0,
//   AsyncStorage,    // '@react-native-community/async-storage'
// }

const persistedReducer = persistReducer(persistConfig, reducer);
4
  • Can you show us how you're declaring your persistConfig using @react-native-community/async-storage? Commented Oct 28, 2019 at 14:13
  • @Neeeko I've updated the question (commented out) to answer your question. Thank you! Commented Oct 28, 2019 at 15:58
  • Are you having this issue on both devices or iOS only? Commented Oct 29, 2019 at 12:59
  • @Neeeko iOS only. No android device currently available for testing this. Commented Oct 29, 2019 at 14:40

5 Answers 5

8

This import is for Web App :

import storage from 'redux-persist/lib/storage'

For React Native, you should use the following imports:

import AsyncStorage from '@react-native-community/async-storage';
import { createMigrate, persistStore, persistReducer } from "redux-persist";
import reducer from "../reducers";

const persistConfig = {
    key: 'root',
    version: 0,
    //...
    storage: AsyncStorage
}

const persistedReducer = persistReducer(persistConfig, reducer);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks tolotra! I did try your approach (commented out on question) but encountered the error [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null. Maybe because I am using Expo SDK 35 which uses React Native 0.5.9 and not 0.6?
May be, try to use expo import for AsyncStorage import { AsyncStorage } from 'react-native';
I have tried this approach, encountered the error redux-persist: config.storage is required. Try using one of the provided storage engines import storage from 'redux-persist/lib/storage'. Updated the question to reflect this.
6

For React Native below code does not work.

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

const persistConfig = {
    key: 'root',
    AsyncStorage
}

Because persistConfig expects a key storage

Below persistConfig works for React Native.

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

const persistConfig = {
    key: 'root',
    storage: AsyncStorage
}

1 Comment

I'm using expo ~45.0.0. So far, this is the solution that works. @react-native-community/async-storage this doesn't work since it can't link up.
1

A simple inadvertent typo such as

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

will end up with

config.storage is required

or

redux-persist failed to create sync storage. falling back to noop storage.

2 Comments

How did you resolve this?
Simply remove { }
0

It might be a library linking issue.

Make sure you ran react-native link @react-native-community/async-storage

I would also suggest going to your ios folder and running pod install.

If you're not using Cocoapods, have a look here for manual linking.

1 Comment

Thanks! Hoping for a solution without having to eject the Expo app (necessary for linking the lib)
0
  1. yarn add @react-native-async-storage/async-storage
  2. import AsyncStorage from '@react-native-async-storage/async-storage'; const authPersistConfig = { key: 'auth', stateReconciler: autoMergeLevel2, storage: AsyncStorage, whitelist: ['isAuthenticated', 'sessionToken', 'userId'], };

worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.