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.
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 .
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.
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.
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);
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'] })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With