Redux-persist 没有正确地在 React Native 应用程序中持久化状态

Redux-persist not persisting state in React Native app properly

我有一个使用 redux 和 redux-persist 的 React 本机应用程序 (v68.1)。

我打算只保留 auth reducer。却只能让它坚持 整个实体减速器。所以我尝试将项目和 todoItems 列入黑名单,但它不起作用。

有什么想法吗? 提前致谢。

Entities.js 

import { combineReducers } from "redux";
import projectsReducer from "./projects";
import uiReducer from "./ui";
import todoItemsReducer from "./todoItems";
import authReducer from "./auth";

export default combineReducers({
  projects: projectsReducer,
  ui: uiReducer,
  todoItems: todoItemsReducer,
  auth: authReducer,
});

reducer.js

import { combineReducers } from "redux";
import entitiesReducer from "./entities";

export default combineReducers({
  entities: entitiesReducer,
});


ConfigureStore.js

import {
  persistStore,
  persistReducer,
} from "redux-persist";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { configureStore, getDefaultMiddleware } from "@reduxjs/toolkit";
import reducer from "./reducer";
import logger from "./middleware/logger";
import api from "./middleware/api";

const persistConfig = {
  key: ‘root’,
  storage: AsyncStorage,
  blacklist: ["projects", "todoItems"],
};

export default function () {
  const persistedReducer = persistReducer(persistConfig, reducer);

  return configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) =>
      getDefaultMiddleware({
        serializableCheck: { ignoreActions: ["persist/PERSIST"] },
      })
        .concat(logger)
        .concat(api),
  });
}


我最终解决了这个问题,方法是将 auth reducer 从 entities reducer 中取出,然后使用 combineReducers 函数保留 auth reducer。

const authPersistConfig = {
  key: "auth",
  storage: AsyncStorage,
};

export default function () {
  const rootReducer = combineReducers({
    auth: persistReducer(authPersistConfig, authReducer),
    entities: entitiesReducer,
  });

  return configureStore({
    reducer: rootReducer,
    middleware: (getDefaultMiddleware) =>
      getDefaultMiddleware({
        serializableCheck: { ignoreActions: ["persist/PERSIST"] },
      })
        .concat(logger)
        .concat(api)
        .concat(toast),
  });
}