Coder Social home page Coder Social logo

Comments (8)

rt2zz avatar rt2zz commented on May 14, 2024

This definitely looks like a bug introduced by some for in changes we made. I would prefer to stay away from munging the proto. Perhaps if we detect the object is not plain we can just wholesale set the substate instead of doing for in?

from redux-persist.

baabgai avatar baabgai commented on May 14, 2024

Maybe instead of hacking with proto we could just use Object.create instead of the object literal initialization of subState.
Assuming reducedState, subState and data are both objects of the same type the code could look like

...
var subState = Object.create(Object.getPrototypeOf(data))
for (var subkey in reducedState[key]) {
    if (reducedState[key].hasOwnProperty(subkey)) subState[subkey] = reducedState[key][subkey]
}
for (var datakey in data) {
    if (data.hasOwnProperty(datakey)) subState[datakey] = data[datakey]
}
autoReducedState[key] = subState
...

This should equally work for immutable and plain objects and might be a cleaner and more official way to do it I guess.

from redux-persist.

rt2zz avatar rt2zz commented on May 14, 2024

how about: https://github.com/rt2zz/redux-persist/blob/master/src/autoRehydrate.js#L38

With isPlainObject check, immutable objects would be wholesale copied instead of shallow merged

from redux-persist.

rt2zz avatar rt2zz commented on May 14, 2024

ok I pushed the isPlainObject change to master let me know if that solves the issue

from redux-persist.

baabgai avatar baabgai commented on May 14, 2024

ok.
After a few more tests I figured that's actually a much better idea since no data is saved on the first level anyhow for immutables, so a shallow merge done like I suggested would actually always overwrite one immutable object with the other one and not merge anything.

from redux-persist.

rt2zz avatar rt2zz commented on May 14, 2024

After messing with this more and working on 2.0, I think this is the correct approach. Thanks for reporting!

from redux-persist.

arilitan avatar arilitan commented on May 14, 2024

I'm running into an issue where one of my reducers is storing state as immutable objects. When I rehydrate the store however, it is saving as a plain object not an immutable one. Any ideas how to fix?

from redux-persist.

rt2zz avatar rt2zz commented on May 14, 2024

@arilitan this should work for you: https://github.com/rt2zz/redux-persist-transform-immutable

from redux-persist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.