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

谁说我不能喝 提交于 2019-12-23 17:50:51

问题


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.


回答1:


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'] })


来源:https://stackoverflow.com/questions/42018091/how-can-i-use-redux-persist-for-only-one-reducer-not-for-navigation-reducer-in-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!