Mislav
Mislav

Reputation: 628

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

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.

Upvotes: 0

Views: 1049

Answers (1)

Husein
Husein

Reputation: 540

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.

Upvotes: 1

Related Questions