嵌套Redux持久器

时间:2020-03-09 08:51:55

标签: javascript redux redux-persist

我有一个具有以下结构的Redux商店:

{ui: {
    drawer: false,
    dialog: false
    },
other_0: {
    other_0_0: false,
    other_0_1: 'and'
    },
other_1: {
    other_1_0: null,
    other_1_1: true
    }
}

我只想保留钥匙抽屉。

到目前为止,我的代码:

import { applyMiddleware, combineReducers, createStore } from "redux";
import thunk from "redux-thunk";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import logger from "redux-logger";
import other_0_reducer from "./other_0_reducer";
import other_1_reducer from "./other_1_reducer";
import ui_reducer from "./ui_reducer";

const rootReducer = combineReducers({
  other_0: other_0_reducer,
  other_1: other_1_reducer,
  ui: ui_reducer,
});

const pConfig = {
  key: "root",
  storage: storage,
  whitelist: ["ui"]
};

const pReducer = persistReducer(pConfig, rootReducer);

const store = createStore(pReducer, applyMiddleware(thunk, logger));
let persistor = persistStore(store);

export { store, persistor };

这使父键“ ui”保持活动状态,但实际上我想将子“ dialog”列入黑名单。

基本上,这是一个嵌套的持久性:我在StackOverflow上看过其他文章,但无法使其正常工作。有人能帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以分别保留每个reducer。

import { applyMiddleware, combineReducers, createStore } from "redux";
import thunk from "redux-thunk";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import logger from "redux-logger";
import other_0_reducer from "./other_0_reducer";
import other_1_reducer from "./other_1_reducer";
import ui_reducer from "./ui_reducer";

const uiPersistConfig = {
  key: "ui",
  storage: storage,
  whitelist: ["drawer"]
}

const rootReducer = combineReducers({
  other_0: other_0_reducer,
  other_1: other_1_reducer,
  ui: persistReducer(uiPersistConfig, ui_reducer)
});

const pConfig = {
  key: "root",
  storage: storage,
  whitelist: []
};

const pReducer = persistReducer(pConfig, rootReducer);

const store = createStore(pReducer, applyMiddleware(thunk, logger));
let persistor = persistStore(store);

export { store, persistor };

希望这会有所帮助。