简体   繁体   English

使用redux-persist持久化嵌套状态对象

[英]Persist nested state object using redux-persist

I am trying to persist my state using redux-persist API. 我正在尝试使用redux-persist API保持状态。 I have my state structure as below. 我的状态结构如下。

var initState = {
  searchInp: "",
  allProducts: {},
  isProductDtlsLoading: true
};

Where allProducts is a nested objects array with each object structure as below : 其中allProducts是具有每个对象结构的嵌套对象数组,如下所示:

allProducts : {
004: {
   defaultOffer: null
   mrp: 550
   productData: [{…}]
   productName: "Hair Dryer"
   productQty: 0
   sellingPrice: 450
   prodCode: "004"
   }
}

Now when I try to persist the data, I can see that Chrome Developer Tools in Application tab, the value for searchInp persists fine and is not lost due to page refresh. 现在,当我尝试保留数据时,我可以看到“应用程序”选项卡中的Chrome开发者工具, searchInp的值可以searchInp保留,并且不会由于页面刷新而丢失。 Value for allProducts is updated fine in the persisted store but then when refreshed the value gets lost and let's say productQty defaults to 0. How can I persist nested object properties like productQty in this case? allProducts的值会在持久性存储中很好地更新,但是刷新后该值会丢失,并且假设productQty默认为0。在这种情况下,如何持久保存诸如productQty类的嵌套对象属性?

index.js index.js

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import rootReducer from "./Store/Reducers/reducer";
import thunk from "redux-thunk";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/es/storage/session";
import { PersistGate } from "redux-persist/lib/integration/react";
import hardSet from "redux-persist/lib/stateReconciler/hardSet";

const persistConfig = {
  key: "root",
  storage: storage,
  stateReconciler: hardSet
};

var pReducer = persistReducer(persistConfig, rootReducer);

var store = createStore(pReducer, applyMiddleware(thunk));
var persistor = persistStore(store);

var app = (
  <Provider store={store}>
    <PersistGate persistor={persistor} loading={null}>
      <App />
    </PersistGate>
  </Provider>
);

ReactDOM.render(app, document.getElementById("root"));

我的应用程序中有非常复杂的状态,这种方式对我来说很好: https//stackoverflow.com/a/37690899/7317796

Most probably you're mutating your state instead of replacing with new one. 很可能您是在改变状态,而不是用新状态替换。 redux-persist relies on the fact that the state is immutable and it should persist the changes correctly unless they have been mutated, see some discussion here https://github.com/rt2zz/redux-persist/issues/623 . redux-persist依赖于以下事实:状态是不可变的,除非更改已被更改,否则状态应该正确地保留更改,请参见此处的一些讨论https://github.com/rt2zz/redux-persist/issues/623 So make sure your correctly updating your state and with nested objects it can be tricky, here is some info on that Cleaner/shorter way to update nested state in Redux? 因此,请确保您正确更新状态并使用嵌套对象比较麻烦,这是一些有关在Redux中更新嵌套状态的更简洁方法的信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM