redux-persist doesn't keep the state on React Native

935 views Asked by At

I've combined redux-persist with Redux to keep the state between app restarts. Currently, on the dev build, it never keeps the state.

My store

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  blacklist:['users', 'activity', 'meeting', 'workshop', 'predavanja', 'klizni', 'meetings', 'standing']
}

const allReducers = combineReducers({
  darkMode: persistReducer(persistConfig, darkReducer),
  users: userReducer,
  activity: activity,
  meeting: meeting,
  workshop: workshop,
  predavanja: predavanja,
  klizni: klizni,
  meetings: meetings,
  standing: standing,
});

I keep logging the state of the state I want to persist but it keeps returning to the default state.

1

There are 1 answers

1
Husein On BEST ANSWER

Is there any reason, you decided to blacklist each reducer in the config? If you are trying to persist all your Redux state, you should not define any blacklist. Furthermore, I can't see, how you are creating your redux store, but it should look something like

import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
...
const store = createStore(persistReducer(persistConfig, allReducers));
const persistor = persistStore(store);

In your root component, you should inject the store like the following:

import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
...
return (
  <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
         .....
      </PersistGate>
   </Provider>
);

The reducers should look like in the following code snippet:

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

const allReducers = combineReducers({
  darkMode: darkReducer,
  users: userReducer,
  activity: activity,
  meeting: meeting,
  workshop: workshop,
  predavanja: predavanja,
  klizni: klizni,
  meetings: meetings,
  standing: standing,
});

If you follow all these steps, your whole redux store should be persisted.