本文介绍了如何在我的本机应用程序中仅将 redux persist 用于一个减速器而不用于导航减速器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Redux persist 来为 reducers 保留我的存储.我的应用程序有两个 reducer Navigation 和 User Reducer.我只想保留用户状态,但我必须根据 redux persist 的这种语法保留这两种状态.

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)

我在create store中传入的reducer是combined reducers的对象.

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.

我在我的应用中使用带有 redux 的导航实验,并且只想保留用户状态.

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

推荐答案

redux-persist v5

PersistConfig 中设置黑名单/白名单,并在 persistCombineReducers(第一个参数)中使用它:

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 可以有一个可选的配置对象作为第二个参数.在配置对象中,您可以定义要忽略的减速器黑名单,或要包含的减速器白名单(并忽略其余的).两者都接受一组 reducer 的键名.

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.

您可以将 navigation(或您的任何名称)reducer 列入黑名单:

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

persistStore(store, { blacklist: ['navigation'] })

这篇关于如何在我的本机应用程序中仅将 redux persist 用于一个减速器而不用于导航减速器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 08:07