如何在 redux-persist v5 中使用白名单?

Hon*_*iao 3 javascript reactjs redux react-redux redux-persist

我尝试在 redux-persist v5 中使用白名单。

我在 Stack Overflow 上关注了这个答案,如下所示

persistStore(store, { whitelist: ['messages'] });
Run Code Online (Sandbox Code Playgroud)

但是,我收到了错误

传递给 persistStore 的选项无效:“白名单”。您可能错误地将 persistConfig 传递到 persistStore 中,而它应该传递到 persistReducer 中。

然后我在阅读 redux-persist 自述文件后尝试了类似下面的内容

const transform = createTransform(null, null, {
  whitelist: [
    'messages'
  ]
});

const config = {
  key: 'state',
  storage: localForage,
  transforms: [transform]
};

const reducer = persistReducer(config, rootReducer);
Run Code Online (Sandbox Code Playgroud)

但这不起作用。它仍然通过 localForage 保存所有内容。

那么在 redux-persist v5 中使用白名单的正确方法是什么?

Ori*_*ori 7

If you're using redux-persist v5, you should pass the PersistConfig, as the 1st parameter of persistCombineReducers:

const config = {
  key: 'root',
  whitelist: ['messages']
}

const reducer = persistCombineReducers(config, reducers)
Run Code Online (Sandbox Code Playgroud)

Note: they've made several changes in v5, read the docs carefully to create the enhanced store, and optionally using the PersistGate.