Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List to Map, after rehydrate. #22

Open
cbjs opened this issue May 2, 2017 · 13 comments
Open

List to Map, after rehydrate. #22

cbjs opened this issue May 2, 2017 · 13 comments

Comments

@cbjs
Copy link

cbjs commented May 2, 2017

redux-persist: 4.6.0
redux-persist-transform-immutable: 4.2.0
immutable: 3.8.1

1

everything is ok, with react-native@0.43.4,
it happens when upgrading to react-native@0.44.0

@tdurand
Copy link

tdurand commented May 3, 2017

I have the same pb, did you find a workaround ?

@cbjs
Copy link
Author

cbjs commented May 3, 2017

no.
I write a custom transformer, to fix this issue temporarily.

@tdurand
Copy link

tdurand commented May 3, 2017

Ok, well if you wanna share it after writing it it's welcome, I'll keep RN 0.43.4 for now.

@cbjs
Copy link
Author

cbjs commented May 3, 2017

not an efficient one. @tdurand

import { createTransform } from 'redux-persist'

export const createFixTransform = () => {
  
  function inbound (state, key) {
    return state;
  }

  function _isList(value) {
    if (!value.keySeq) return false;

    let res = true, sum = 0, ksum = 0;
    value.keySeq().forEach((v, k) => {
      if (typeof v !== 'number') {
        res = false;
        return false;
      }
      sum += v;
      ksum += k;
      return res;
    })

    return res && sum == ksum;
  }

  function _traverse(value) {
    if (value && value.map) {
      let newValue = value.map((v) => _traverse(v));
      if (_isList(newValue)) return newValue.toList();
      return newValue;
    }

    return value;
  }

  function outbound (state, key) {
    if (!state) return state;
    return _traverse(state);
  }

  return createTransform(inbound, outbound);
};

    persistStore(store, {
      storage: AsyncStorage,
      transforms: [createFixTransform(), immutableTransform()]
    });

@tdurand
Copy link

tdurand commented May 3, 2017

Thanks !

@rt2zz
Copy link
Owner

rt2zz commented May 4, 2017

that is curious. If you figure out an alternative please post. Perhaps updating deps across the board will help.

@johanmic
Copy link

johanmic commented May 9, 2017

bumped everything to latest versions and having the same issue

original: ["email", "facebook"]
after REHYDRATE: {0: "email", 1: "facebook"}

"redux-persist": "^4.8.0",
"redux-persist-transform-immutable": "^4.3.0",
"react": "16.0.0-alpha.6",
"react-native": "^0.44.0",
"immutable": "^3.8.1",

@jeffberry
Copy link

I had this same problem, I ended up figuring out it was because react native had it's own instance of immutable installed in node_modules/react-native/node_modules/immutable. I was using a different version of Immutable in my app's package.json; once I switched that version to the same version react-native requires (~3.7.6) the second installation in the react-native folder went away, and my issues were fixed. Thought this might be helpful to some.

@giautm
Copy link

giautm commented Jun 15, 2017

Same here. :(

@turnerhayes
Copy link

For some reason, the first time the transit-js marshaller encounters an immutable List, it has a write handler registered to it, but subsequent encounters, the List constructor is a different one from the initial one, so it has no handler and uses the default handler, which is to treat it as a Map. I haven't figured out why the List constructor that gets returned is different the subsequent times, though (if I compare the constructor to Immutable.List, it's equal the first time and not equal subsequent times). This may be a bug with the transit-immutable-js library.

@rt2zz
Copy link
Owner

rt2zz commented Oct 27, 2017

this issue will likely be resolved by switching serialize libs #25

Rolling this out however is non trivial since it will break backwards compat. We may need to ship it as a new module.

@turnerhayes
Copy link

Update on my issue; I think the problem was with my setup; I was using an Immutable record from another library (that I own), and that library had immutable as a dependency, so it was creating immutable collections with its own instance of Immutable. The app I was using it from had its own instance of immutable, so there were two instances of Immutable. I changed my library to have immutable as a peerDependency instead and it worked. For the record, it worked with both serialize libs.

@fabriziomoscon
Copy link

@turnerhayes thank you, this solved the issue for us as well. We prefer using peerDependency over switching serialize lib, because in this way we ensure that we use the same version of Immutable throughout our code and libraries used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants