【问题标题】:What are the keys in redux-persist v5 config's blacklist and whitelist?redux-persist v5 配置的黑名单和白名单中的键是什么?
【发布时间】:2017-11-01 15:55:17
【问题描述】:

我试图将我的 redux 存储持久化到 sessionStorage,并且我希望一个 reducer 不持久化。我试图将其名称添加到黑名单数组中,但它仍然保留整个商店。 你们能告诉我配置中的键是什么吗? 在文档中显示如下

{
 key: string, // the key for the persist
 storage: Object, // the storage adapter, following the AsyncStorage api
 version?: number, // the state version as an integer (defaults to -1)
 blacklist?: Array<string>, // do not persist these keys
 whitelist?: Array<string>, // only persist they keys
 migrate?: (Object, number) => Promise<Object>,
 transforms?: Array<Transform>,
 throttle?: number,
 keyPrefix?: string, // will be prefixed to the storage key
 debug?: boolean, // true -> verbose logs
 stateReconciler?: false | StateReconciler, // false -> do not 
 automatically reconcile state
}

【问题讨论】:

    标签: javascript reactjs redux redux-persist


    【解决方案1】:

    它指的是你的 sub reducer 键,但深一层(这可能是你的问题)。

    一个例子

    你像这样创建你的主减速器

    export default combineReducers({
      app,
      auth,
      comments
    });
    

    如果你只想持久化你的comments reducer,你可以使用whitelist: ['comments']blacklist: ['app', 'auth']。 如果你想将更深层次的东西列入黑名单或白名单,你应该看到 redux-persist-transform-filter。

    【讨论】:

      【解决方案2】:

      你在 redux 中的 index.js 应该是这样的:

      import {persistCombineReducers} from 'redux-persist';
      import storage from 'redux-persist/es/storage';
      
      // You have to import every reducers and combine them.
      import {reducer as UserReducer} from './UserReducer';
      import {reducer as CommentReducer} from './CommentReducer';
      import {reducer as ProductRedux} from './ProductRedux';
      
      const config = {
        key: 'root',
        storage,
        blacklist: [
          'Comment',
          'Product',
        ],
      };
      
      export default persistCombineReducers(config, {
        user: UserReducer,
        comment: CommentReducer,
        product: ProductRedux,
      });
      

      【讨论】:

        猜你喜欢
        • 2018-04-09
        • 2010-11-30
        • 1970-01-01
        • 2017-05-03
        • 2018-03-28
        • 2015-01-03
        • 1970-01-01
        • 2020-04-14
        • 1970-01-01
        相关资源
        最近更新 更多