React native redux-persist:如何忽略键(黑名单)?

新手上路,请多包涵

我正在使用 redux-persist 存储我的设置,并想忽略其中的一些设置以便在每次重启时重置它们,例如在崩溃之后。

可以将 reducer 名称数组添加为 blacklistwhitelist ,但我想忽略特定的键,例如 settings.isLoggedIn settings

 // ...
function configureStore(initialState) {
    const store = createStore(
        RootReducer,
        initialState,
        enhancer
    );

    persistStore(store, {
        storage: AsyncStorage,
        blacklist: ['router', 'settings'] // works, 'settings.isLoggedIn' doesn't.
    }, () => {
        // restored
    });

    return store;
}
// ...

我是否必须创建另一个减速器或者有人可以解决这个问题吗?

提前致谢!

原文由 Mr. B. 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 689
2 个回答

根据 文档,黑名单参数包含:’keys (read: reducers) to ignore’,所以恐怕无法实现您想要的行为。您可以尝试自己实现该功能,但我认为该包的代码库真正关注的是将 reducer 列入黑名单,而不是属性(参见 this )。恐怕唯一的解决方案是为您的非持久键创建一个单独的 reducer(根据我的经验,这并不是一件麻烦事)。

原文由 martinarroyo 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以为此使用 Nested Persists

 import { persistStore, persistReducer } from 'redux-persist';

const rootPersistConfig = {
  key: 'root',
  storage: storage,
  blacklist: ['auth']
}

// here you can tell redux persist to ignore loginFormData from auth reducer

const authPersistConfig = {
  key: 'auth',
  storage: storage,
  blacklist: ['loginFormData']
}

// this is your global config
const rootReducer = combineReducers({
  auth: persistReducer(authPersistConfig, authReducer),
  other: otherReducer,
})

// note: for this to work, your authReducer must be inside blacklist of
// rootPersistConfig

const myReducerConfig = {
  key: "cp",
  storage: storage,
  blacklist: ["authReducer"],
  debug: true
};

原文由 shubham choudhary 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题