85

I'm trying to setup redux-persist in a react native app.

However I'm hitting this error:

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

I've tried to change the storage from storage to AsyncStorage in "src/redux/index.js", but it's still hitting the same error:

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

const config = {
  key: "root",
  storage: AsyncStorage // Attempted to fix it (but failed)
  // storage // old code
};

Here's the other codes:

In App.js:

import React, { Component } from "react";
import { Provider } from "react-redux";
import { persistStore } from "redux-persist";
import { PersistGate } from "redux-persist/es/integration/react";
import store from "@store/configureStore";
import Router from "./src/Router";

export default class ReduxWrapper extends Component {
  render() {
    const persistor = persistStore(store);
    return (
      <Provider store={store}>
        <PersistGate persistor={persistor}>
          <Router />
        </PersistGate>
      </Provider>
    );
  }
}

In configureStore.js:

import { applyMiddleware, compose, createStore } from "redux";
import thunk from "redux-thunk";
import reducers from "@redux";

const middleware = [
  thunk,
  // more middleware
];

const configureStore = () => {
  let store = null;
  store = compose(applyMiddleware(...middleware))(createStore)(reducers);
  return store;
};

export default configureStore();

In /src/redux/index.js

import { persistCombineReducers } from "redux-persist";
import storage from "redux-persist/es/storage";

import { reducer as NetInfoReducer } from "./NetInfoRedux";
import { reducer as UserRedux } from "./UserRedux";

const config = {
  key: "root",
  storage,
};

export default persistCombineReducers(config, {
  netInfo: NetInfoReducer,
  user: UserRedux,
}

In Router.js

import React from "react";
import NetInfo from "@react-native-community/netinfo/lib/commonjs";
import { Config, AppConfig, Device, Styles, Theme, withTheme } from "@common";
import { AppIntro } from "@components";
import { connect } from "react-redux";

class Router extends React.PureComponent {
  constructor(props){
    super(props)
    this.state = {
      loading: true,
    };
  }

  componentWillMount() {
    NetInfo.getConnectionInfo().then((connectionInfo) => {
      this.props.updateConnectionStatus(connectionInfo.type != "none");
      this.setState({ loading: false });
    });
  }

  render() {
    return <AppIntro />;
  }
}

export default withTheme(
    connect(
    //   mapStateToProps,
    //   mapDispatchToProps
    )(Router)
);

Update:

Managed to solve the error base on mychar suggestion: github.com/rt2zz/redux-persist/issues/1080:

  1. npm install --save @react-native-community/async-storage

  2. on iOS, remember to perform "pod install" in iOS folder

  3. Change storage to AsyncStorage

    old code => import storage from 'redux-persist/lib/storage'; new code => import AsyncStorage from '@react-native-community/async-storage';

    old code => const persistConfig = { //... storage, }

    new code => const persistConfig = { //... storage: AsyncStorage, }

However, still getting this warning:

enter image description here

Blue Bot
  • 2,278
  • 5
  • 23
  • 33
user1872384
  • 6,886
  • 11
  • 61
  • 103
  • what is your react-native version ? Did you link the @react-native-community/async-storage – cain Sep 04 '19 at 07:17
  • 0.60.5... No I didn't like the async-storage – user1872384 Sep 04 '19 at 07:45
  • check this https://github.com/rt2zz/redux-persist/issues/1080 – mychar Sep 04 '19 at 08:37
  • 2
    @mychar, many thanks. The abovementioned error is gone, however I'm still hitting a warning, do you know how to get rid of this warning? "Warning: Async Storage has been extracted from react-native core and will be removed in a future release. It can now be installed and imported from '@react-native-community/async-storage' instead of 'react-native'. See https://github.com/react-native-community/react-native-async-storage" – user1872384 Sep 04 '19 at 14:15
  • 1
    @user1872384, how do you remove the console error. still, I am getting console error – mychar Sep 04 '19 at 18:01
  • @mychar which console error? Do you mean this "console.error: "redux-persist failed to create sync storage. falling back to "noop" storage"? I've followed the solution in the link you've provided me lol... I've updated the steps I've perform in the question above. Remember to run pod install in iOS folder – user1872384 Sep 05 '19 at 03:06
  • @user1872384, still I am getting console error. I have done all the steps. but still having the same issue. also done the pod install too. don't know why – mychar Sep 05 '19 at 03:37
  • Change the PodSpec file inside nodemodule/@react-native-community/async-storage "Rect" to "Rect-Core"and pod install – cain Sep 05 '19 at 04:15
  • @mychar I wish I can return you a favour by helping you to solve the issue. Are you able to create a new project with minimal code to get redux-persist up and running? – user1872384 Sep 05 '19 at 05:53
  • @user1872384 thanks for the help, the error was solved. – mychar Oct 07 '19 at 15:11

10 Answers10

112

In redux-persist v6, you try changing as follows:

Old config V5 =>

import storage from 'redux-persist/lib/storage';
const persistConfig = {
   //...
   storage,
}

New config v6 =>

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

import AsyncStorage from '@react-native-async-storage/async-storage';
const persistConfig = {
  //...
  storage: AsyncStorage,
}
rybo111
  • 12,240
  • 4
  • 61
  • 70
rfdc
  • 1,665
  • 1
  • 10
  • 15
  • 7
    and don't forget to do this after adding async-storage package: cd ios/ && pod install – merry_ejik Sep 11 '19 at 07:17
  • 2
    thx... still doens't solve this though: ""Warning: Async Storage has been extracted from react-native core and will be removed in a future release. It can now be installed and imported from '@react-native-community/async-storage' instead of 'react-native'." – user1872384 Oct 29 '19 at 02:13
  • @user1872384 check my detailed answer why you are receiving that error – Edison Biba Dec 19 '19 at 12:28
  • @merry_ejik what if one doesn't have an ios directory in an expo project? – zengod Jan 14 '20 at 20:07
  • Assuming your app is Expo managed(created via expo-cli), instead of using the AsyncStorage from Facebook RN Documentation use: import { AsyncStorage } from 'react-native'; – Utkarsh Apr 26 '20 at 14:48
  • 6
    Don't forget to remove `import storage from "redux-persist/es/storage"` line. – Md. Robi Ullah Jul 01 '20 at 06:54
  • 1
    It's worth mentioning that `@react-native-community/async-storage` has been deprecated and moved to a new organization with the name `@react-native-async-storage/async-storage` so you might as well replace the import statement in config v6 => with: `import AsyncStorage from '@react-native-async-storage/async-storage';` – Ali Abdelfattah Dec 17 '20 at 21:03
28

I faced the same issue, even though I replaced storage with AsyncStorage.

I solved the issue by removing the import line of the original storage

import storage from "redux-persist/es/storage"; //remove this
Avindu Hewa
  • 1,608
  • 1
  • 15
  • 23
25

Before redux-persist v6.0.0 you were using storage like this:

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

What this uses in background is AsyncStorage that was in react-native core.

Since react-native has deprecated AsyncStorage and will remove it from react-native core then the new version of redux-persist has stopped using it and it seems a good decision.

You can do the same now but instead import AsyncStorage from community version.

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

And then use in in your config:

const persistConfig = {
  storage: AsyncStorage,
  //other configurations
};

Downgrading to redux-persist v5 is not a stable solution since it uses AsyncStorage from core react-native and react-native will remove it completely in the upcoming versions.

Also I read in comments that you don't like AsyncStorage, well as I explained redux-persist has been using it as a storage so the only difference now is that you should get it from community version and not from react-native core

Edison Biba
  • 4,384
  • 3
  • 17
  • 33
14

First of all import AsyncStorage using below line

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

Secondly the persistConfig should be assigned AsyncStorage like below and if you supplied storage object then comment it out

const persistConfig = {
    // storage,
    storage: AsyncStorage,  
}

Lastly the below line importing storage or any other storage should not exist in your file

// import storage from 'redux-persist/lib/storage'  COMMENT THIS OUT

This is how I solved mine problem.

12

For Expo SDK version >=33 Workflow, the plugin @react-native-community/async-storage doesn't work.

It worked for me.

import { AsyncStorage } from 'react-native';`  
.  
.   
.  
const persistConfig = {
  key: 'root',
  storage: AsyncStorage
}

reference: https://docs.expo.io/versions/latest/react-native/asyncstorage/

Henry Palacios
  • 301
  • 3
  • 8
9

I solve it by removing :

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

and replacing it with:

import { AsyncStorage } from 'react-native'

Don't forget this :

const rootPersistConfig = {  
    key: 'root',
    storage: AsyncStorage
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
2

My error was solved by downgrading redux-persis version to "5.10.0".

Sardar Usama
  • 97
  • 1
  • 11
2

Comment out / exclude this line from your imports import storage from 'redux-persist/lib/storage'

ensure that only async storage is imported

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

1

comment out //import storage from "redux-persist/lib/storage";

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

mckenytech
  • 11
  • 1
-3

Downgrade redux-persist to 5.10.0

Chinedu Ofor
  • 707
  • 9
  • 11