Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use redux persist for only one reducer not for navigation reducer in my react native application?

I am using Redux persist to persist my store for reducers. My application has two reducers Navigation and User Reducer.I want to persist only user state but I have to persist both states according to this syntax of redux persist.

          import {persistStore, autoRehydrate} from 'redux-persist'
          const store = createStore(reducer, undefined, autoRehydrate())
          persistStore(store)

the reducer that I have passed in the create store is the object of combine reducers.

Problem: The problem I am getting through this is when I exit my app, as the navigation state is also persist now, my app opens the same page from which I exited my app.

I am using navigation experimental with redux in my app and want to persist only user state.

like image 448
coderzzz18 Avatar asked Feb 03 '17 06:02

coderzzz18


People also ask

Can I use redux persist in react native?

Redux Persist and React NativeIn React Native applications, data can be persisted locally using AsyncStorage . AsyncStorage is an asynchronous, persistent, key-value storage system that is global to the entire app. Redux Persist is a tool used to seamlessly save the application's Redux state object to AsyncStorage .

What is redux persist used for?

With the Redux Persist library, developers can save the Redux store in persistent storage, for example, the local storage. Therefore, even after refreshing the browser, the site state will still be preserved.

Can we have multiple reducers in react?

Having multiple reducers become an issue later when we create the store for our redux. To manage the multiple reducers we have function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them.

How do I make redux persist blacklist for nested state?

const persistConfig = { key: 'root', storage: AsyncStorage, } const persistedReducer = persistReducer(persistConfig, reducers) const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore); export const store = createStoreWithMiddleware(persistedReducer); export const persistor = persistStore(store);


1 Answers

redux-persist v5

Set the blacklist/whitelist in the PersistConfig, and use it in persistCombineReducers (the 1st param):

const persistConfig = {
  key: 'root',
  blacklist: ['navigation'],
  storage,
}

const reducer = persistCombineReducers(persistConfig , reducers)

redux-persist v4

persistStore can have an optional config object as the 2nd parameter. In the config object, you can define a black list of reducers to ignore, or a white list of reducers to include (and ignore the rest). Both accept an array of reducers' key names.

You can blacklist the navigation (or whatever yours is called) reducer:

persistStore(store, { blacklist: ['navigation'] })
like image 59
Ori Drori Avatar answered Sep 20 '22 17:09

Ori Drori