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

combineReducers converts Immutable Map into plain JS object #1894

Closed
sarink opened this issue Aug 12, 2016 · 5 comments
Closed

combineReducers converts Immutable Map into plain JS object #1894

sarink opened this issue Aug 12, 2016 · 5 comments

Comments

@sarink
Copy link

sarink commented Aug 12, 2016

According to my reading, this should no longer happen in redux v3? Or am I doing something wrong?

myReducer.js

import { Map } from 'immutable';

const initialState = Map({});

export default (state = initialState, action = {}) => {
  switch (action.type) {
    default:
      // On the third run "@@INIT" the state gets converted from a Map to a plain JS object :(
      console.log('action:', action.type, 'state:', state);
      return state;
  }
};

output

action: @@redux/INIT
state: Map {size: 0, _root: undefined, __ownerID: undefined, __hash: undefined, __altered: false}

action: @@redux/PROBE_UNKNOWN_ACTION_x.5.a.b.w.u.f.k.8.3.q.b.s.5.e.e.4.5.c.d.i
state: Map {size: 0, _root: undefined, __ownerID: undefined, __hash: undefined, __altered: false}

action: @@INIT
state: Object {}
@markerikson
Copy link
Contributor

Uh.... no, combineReducers only knows how to work with plain JS objects. Not sure why you think this has changed at some point. If you need a similar function that knows how to work with an Immutable Map, there's a number of utilities along that line listed at https://github.com/markerikson/redux-ecosystem-links/blob/master/immutable-data.md#immutableredux-interop .

There has been some discussion of making combineReducers configurable so it could iterate over a plain JS object, an Immutable map, or something else if needed, but that seems to be stalled at this point. See #1792 .

@sarink
Copy link
Author

sarink commented Aug 12, 2016

Ah, I guess I misunderstood. Sorry and thanks for the clarification.

@sarink
Copy link
Author

sarink commented Aug 12, 2016

Is this still the case if my state is a plain object but some of the keys within state are Immutables?

@markerikson
Copy link
Contributor

That should work okay. The point is that combineReducers does:

var finalReducerKeys = Object.keys(finalReducers)

// later
    for (var i = 0; i < finalReducerKeys.length; i++) {
      var key = finalReducerKeys[i]
      var reducer = finalReducers[key]

Which obviously only works with plain objects. It's the iteration over the object that's the issue - if the values at each key are Immutable objects, that's fine as long as the slice reducer can handle those.

@sarink
Copy link
Author

sarink commented Aug 12, 2016

This is exactly how my setup is, because we're slowly transitioning to Immutable. So... This shouldn't be a problem. What's wrong?

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

2 participants