Persist nested state object using redux-persist











up vote
1
down vote

favorite












I am trying to persist my state using 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 : {
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.
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?



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"));









share|improve this question


























    up vote
    1
    down vote

    favorite












    I am trying to persist my state using 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 : {
    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.
    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?



    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"));









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to persist my state using 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 : {
      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.
      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?



      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"));









      share|improve this question













      I am trying to persist my state using 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 : {
      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.
      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?



      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"));






      javascript reactjs redux session-storage redux-persist






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 4:42









      Shantanu Tomar

      61762044




      61762044
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I have pretty complex state in my app and this way works for me fine:
          https://stackoverflow.com/a/37690899/7317796






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245915%2fpersist-nested-state-object-using-redux-persist%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I have pretty complex state in my app and this way works for me fine:
            https://stackoverflow.com/a/37690899/7317796






            share|improve this answer

























              up vote
              0
              down vote













              I have pretty complex state in my app and this way works for me fine:
              https://stackoverflow.com/a/37690899/7317796






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I have pretty complex state in my app and this way works for me fine:
                https://stackoverflow.com/a/37690899/7317796






                share|improve this answer












                I have pretty complex state in my app and this way works for me fine:
                https://stackoverflow.com/a/37690899/7317796







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 at 8:05









                Noemi

                313




                313






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245915%2fpersist-nested-state-object-using-redux-persist%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation