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

How can I fully use ImmutableJS? #108

Closed
AsaAyers opened this issue Sep 13, 2015 · 6 comments
Closed

How can I fully use ImmutableJS? #108

AsaAyers opened this issue Sep 13, 2015 · 6 comments

Comments

@AsaAyers
Copy link

The first time I committed to a debug session when I reloaded my state broke because .get() didn't exist on the objects in the store. It seemed like the solution was to provide a stateDeserializer to persistState.

stateSerializer = (state) => Immutable.fromJS(state)

The next level problem is that fromJS only converts arrays to List or objects to Map. If your store relies on Records, Sets or any of the specialized data types, it doesn't really deserialize correctly.

@danielkcz
Copy link
Contributor

Yeah, that's why the stateDeserializer is there. You get the full state in pure JS and it's up to you to convert it to proper Records and other objects. It's not really a DRY solution since you have to specify you structure in another place, but I don't think there is a easy way to overcome that.

@AsaAyers
Copy link
Author

Do you have an actual recommendation on how to solve the problem?

Obviously the state is built up through a tree of combined reducers. I could implement a custom stateDeserializer to do everything manually, but that approach requires stateDeserializer to know the details of how every reducer in my system works and I have to maintain any changes across 2 locations. That sounds like a lot of very fragile code maintained in 2 places, which is made worse by the fact that one is only for development.

@danielkcz
Copy link
Contributor

Yes exactly, it's somewhat fragile, but I don't know about better way for now unfortunately. I have a state deserializer like this.

function deserializer(state) {
    if (!state) {
        return state;
    }
    return {
        ...state,
        tgDocument: Imm.List(state.tgDocument.map((row) => new TGRow(row))),
        tgFocus: new TGZoom(state.tgZoom),
    };
}

@gaearon
Copy link
Contributor

gaearon commented Sep 27, 2015

Closing as inactive.

@gaearon gaearon closed this as completed Sep 27, 2015
@AsaAyers
Copy link
Author

For anyone who finds this in the future: My solution was to give up on Immutable JS and use https://github.com/leoasis/redux-immutable-state-invariant to tell me if I mutate state somewhere.

As long as you turn on es7.objectRestSpread it's easier to work with than Immutable and you just have to remember to be intentional about not mutating state.

@kachkaev
Copy link
Collaborator

kachkaev commented Aug 3, 2016

I'm playing with the boilerplate by @mxstbr, which uses Immutable.js for the entire state and I can't figure out how persistState enhancer should look like in the devtools. Because an node that is being created when I open ?debug_session=42 has a dynamic name, Immutable's combinedReducer fails saying:

The previous state received by the reducer is of unexpected type.
Expected argument to be an instance of Immutable.Iterable with
the following properties: "a", "b", "c", "d".

And it looks like I can't supplement the root reducer with some extra dummy reducer in combineReducers from redux-immutable myself as the name is unpredictable.

I was getting myself familiar with redux-devtools via Walkthrough.md so thought that it would be good to mention the case of an Immutable store there. I've seen some threads in various repos that are trying to approach the issue I face, but it would be probably good to have some hint where there's the highest number of newcomers. @gaearon could you please add a couple of lines of text describing what to use instead of redux-devtools/persistState you your state is Immutable?

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

4 participants