如何使用Redux持久化在所有reducer切片中保持状态?

时间:2019-02-08 15:34:52

标签: javascript react-native react-redux redux-persist react-state-management

我已经添加了,redux坚持到我的应用程序。我有一个根减速器,其中包含不同的减速器,例如“ Auth”,“ Orders”,“ Products”,“ Transactions”等。

问题是我的“订单和产品”简化程序可以正确保存,但是“身份验证和交易”简化程序不能持久保存。

我尝试调试,但在root reducer的Auth和Transactions切片中看不到任何数据

这是我的顶级配置

const persistConfig = {
  key: 'root',
  storage,
  // blacklist: ['navigation']   pass state which you want to ignore
  blacklist: ['navigation', 'Error', 'Address', 'form'],
  stateReconciler: autoMergeLevel2,
};
const pReducer = persistReducer(persistConfig, rootReducer);

导出为

export const configureStore = createStore(pReducer, composeEnhancers(applyMiddleware(thunk)));
export const persistor = persistStore(configureStore);

在我的App.js中


  <Provider store={store}>
      {/* the loading and persistor props are both required! */}
        <PersistGate loading={<Spinner />} persistor={persistor}>
           <CommuneVendorRoot />
       </PersistGate>
   </Provider>

1 个答案:

答案 0 :(得分:0)

import { persistCombineReducers } from 'redux-persist'
import storage from 'redux-persist/es/storage'
import todos from '../reducers/reducers';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';

console.log(todos)
const config = {
  key: 'root',
  storage: storage,
  debug: true,
  stateReconciler: autoMergeLevel2
};


const rootReducer = persistCombineReducers(config, {
  Transactions: todos
});
export default rootReducer;
  

请检查上面的代码,它可以正常工作。告诉我你   仍然有任何问题