react-router-redux icon indicating copy to clipboard operation
react-router-redux copied to clipboard

Support ImmutableJS Map as the state

Open zavelevsky opened this issue 10 years ago • 33 comments

Although it makes sense to use ImmutableJS Map object as your state object - it currently can't be done because the reducer of redux-simple-router uses simple Object.assign. Please consider supporting it by allowing to pass a custom reducer, or a reducer's internal state creator, such as (state, obj) => newState.

zavelevsky avatar Dec 27 '15 10:12 zavelevsky

:+1: I don't want to keep people from being able to use ImmutableJS if they want to with my boilerplate, would be nice if support for this added! (See https://github.com/mxstbr/react-boilerplate/issues/6)

mxstbr avatar Dec 27 '15 12:12 mxstbr

+1 for a custom state access function. That seems to be the idiomatic way this is handled in other redux libraries that provide their own reducer.

neverfox avatar Dec 31 '15 16:12 neverfox

We support a custom state accessor function in https://github.com/rackt/redux-simple-router#syncreduxandrouterhistory-store-selectrouterstate

jlongster avatar Dec 31 '15 16:12 jlongster

Oh, ha. I just took the OP's issue at face value and didn't check that. I wonder what the problem is then. That should be all you need to support any immutable library.

neverfox avatar Dec 31 '15 23:12 neverfox

I'm considering closing this unless the OP can describe further what's wrong. I know several people that use Immutable.js just fine with this.

jlongster avatar Dec 31 '15 23:12 jlongster

Last I checked you return the result of Object.assign from the reducer. This of course returns an object and not the original Map. On Jan 1, 2016 1:52 AM, "James Long" [email protected] wrote:

I'm considering closing this unless the OP can describe further what's wrong. I know several people that use Immutable.js just fine with this.

— Reply to this email directly or view it on GitHub https://github.com/rackt/redux-simple-router/issues/140#issuecomment-168263192 .

zavelevsky avatar Jan 01 '16 05:01 zavelevsky

Original type, which is Map. On Jan 1, 2016 7:27 AM, "Doron Zavelevsky" [email protected] wrote:

Last I checked you return the result of Object.assign from the reducer. This of course returns an object and not the original Map. On Jan 1, 2016 1:52 AM, "James Long" [email protected] wrote:

I'm considering closing this unless the OP can describe further what's wrong. I know several people that use Immutable.js just fine with this.

— Reply to this email directly or view it on GitHub https://github.com/rackt/redux-simple-router/issues/140#issuecomment-168263192 .

zavelevsky avatar Jan 01 '16 05:01 zavelevsky

An original Map of what? Can you be more specific about what you're trying to accomplish?

neverfox avatar Jan 01 '16 05:01 neverfox

Sorry, I'm limited to answering by email at the moment. As I recall, the lib's reducer returns the result of Object.assign, therefore an object. If my state is an ImmutableJS Map then after the reducer is called, what I get is no longer an instance of a Map but a simple object. Tjis might have changed since last I checked, but I cannot check at the moment. Just wanted to clarify so the issue won't be closed prematurely. On Jan 1, 2016 7:49 AM, "Roman Pearah" [email protected] wrote:

An original Map of what?

— Reply to this email directly or view it on GitHub https://github.com/rackt/redux-simple-router/issues/140#issuecomment-168286080 .

zavelevsky avatar Jan 01 '16 06:01 zavelevsky

So what you're asking for is not just for the reducer to be able to get it's necessary sub-state out via a customer selector, but for the ability to tell the reducer how to write the new state back out, e.g. via fromJS()? Is that just because you want to be able to consistently access state values with the ImmutableJS API, e.g. state.getIn(['routing', 'path']) instead of state.get('routing').path?

neverfox avatar Jan 01 '16 07:01 neverfox

Could you not then just do something like:

syncReduxAndRouter(history, store, state => state.get('routing').toJS())

...

combineReducers({
  routing: (state, action) => routeReducer(state, action).fromJS()
})

neverfox avatar Jan 01 '16 07:01 neverfox

Specifically, combineReducers doesn't support immutablejs for the same reason - it assumes the root of your state is a simple object. But yes - I can wrap the reducer and import its result. It's not very efficient but the routing subtree is very small. I'd rather pass my own state setting function. On Jan 1, 2016 9:10 AM, "Roman Pearah" [email protected] wrote:

Could you not then just do something like:

syncReduxAndRouter(history, store, state => state.get('routing').toJS())

...

combindReducers({ routing: (state, action) => routeReducer(state, action).fromJS() })

— Reply to this email directly or view it on GitHub https://github.com/rackt/redux-simple-router/issues/140#issuecomment-168287599 .

zavelevsky avatar Jan 01 '16 07:01 zavelevsky

Specifically, combineReducers doesn't support immutablejs for the same reason - it assumes the root of your state is a simple object.

True, but I assumed that if you're doing what you seem to be doing, then you're already using one of the combineReducers mods from some project like redux-immutablejs or redux-immutablejs, or your own.

So, you want to be able to tell redux-simple-router how to set values in state, right? So instead of:

return { location }

it would be

return setLocation(location)

with that function being provided as:

function setLocation (location) { return Immutable.Map({ location }) }

I don't really see the advantage over fromJS when it requires a code change to the library. If the reducer were more complex, then I imagine you'd still end up just writing a state setting function that uses fromJS() since you couldn't easily account for all the different ways state might be altered (e.g. some places might need set and others merge or updateIn).

neverfox avatar Jan 01 '16 08:01 neverfox

OK, I'm convinced. Thanks, as far as I'm concerned the issue is closed. Keep up the good work! On Jan 1, 2016 10:20 AM, "Roman Pearah" [email protected] wrote:

Specifically, combineReducers doesn't support immutablejs for the same reason - it assumes the root of your state is a simple object.

True, but I assumed that if you're doing what you seem to be doing, then you're already using one of the combineReducers mods from some project like redux-immutablejs or redux-immutablejs, or your own.

So, you want to be able to tell redux-simple-router how to set values in state, right? So instead of:

return { location }

it would be

return setLocation(location)

with that function being provided as:

function setLocation (location) { return Immutable.Map({ location }) }

I don't really see the advantage over fromJS when it requires a code change to the library. If the reducer were more complex, then I imagine you'd still end up just writing a state setting function that uses fromJS() since you couldn't easily account for all the different ways state might be altered (e.g. some places might need set and others merge or updateIn).

— Reply to this email directly or view it on GitHub https://github.com/rackt/redux-simple-router/issues/140#issuecomment-168291583 .

zavelevsky avatar Jan 01 '16 08:01 zavelevsky

So there is no way to use [email protected] with immutablejs?

stepankuzmin avatar Jan 18 '16 14:01 stepankuzmin

Just write your own reducer. Ours is very simple, so you just modify it to store an immutable object.

timdorr avatar Jan 18 '16 15:01 timdorr

Thanks for reply, @timdorr! I've made simple immutable reducer:

import { UPDATE_LOCATION } from 'redux-simple-router'

const initialState = new Immutable.Map({
  location: undefined
})

export default function routeReducer(state = initialState, { type, location }) {
  if (type !== UPDATE_LOCATION) {
    return state
  }

  return state.merge({ location })
}

It works well, but when I apply middleware.listenForReplays(store) I'm getting Uncaught TypeError: Cannot read property 'location' of undefined. Is this approach compliant with redux-devtools?

stepankuzmin avatar Jan 18 '16 17:01 stepankuzmin

Most likely not. The listenForReplays stuff would need to be redone to support Immutable as well.

timdorr avatar Jan 18 '16 18:01 timdorr

Oh, I see. So I'm going back to v1.x :)

stepankuzmin avatar Jan 18 '16 18:01 stepankuzmin

All we need is the location from the state, so we could take a selector getLocationState instead of getRouterState which returns a JS location object. Immutable users would get the location state (an immutable object) and call toJS on it. If you wanted to avoid this perf hit, you could only call listenForReplays in development, as it's only needed in development mode.

jlongster avatar Jan 20 '16 03:01 jlongster