1

I tried to set up react-router-redux in my application, as presented here, but it broke my state.

Here is my store setup :

import { createStore, applyMiddleware } from 'redux'
import { combineReducers } from 'redux'
import thunk from 'redux-thunk'
import { syncHistory, routeReducer } from 'react-router-redux'
import rootReducer from '../reducers'
import Immutable, {Map} from 'immutable'
[Other imports]

const middleware = [X,Y]

const reducer = combineReducers(Object.assign({}, rootReducer, {
  routing: routeReducer
}));

export default function configureStore(initialState, history) {

   const reduxRouterMiddleware = syncHistory(history);
   middleware.push(reduxRouterMiddleware);

   const createStoreWithMiddleware = applyMiddleware(...middleware)(createStore);

   const store = createStoreWithMiddleware(reducer, initialState);     

   return store

}

Here is my reducers/index.js :

import { combineReducers } from 'redux'
import A from './A'
import B from './B'
import C from './C'
...

const rootReducer = combineReducers({
  A,
  B,
  C,
  ...
})

export default rootReducer

When I console.log in my application my store.getState(), I get : only a routing.location tree.

only routing.location

If I use "rootReducer" instead of "reducer" when creating my store, I get my normal state tree (A, B, C ...)

no routing.location

but of course I no longer get the routing.location part I'm interested in ...

What am I doing wrong ?

Thank you all !

2 Answers 2

1

I think it's because you call combineReducer on your own reducers, and then again to merge in the routing reducer. I'd just export an object from your own reducers, and do the combineReducer call once, where you Object.assign the route reducer in.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Steven ! Removing the first call to combineReducers (in reducers/index.js) worked ! As a reducer is already a function, I though could merge a reducers combination with another reducer ... Apparently it's not possible :-) ...
1

I think the problem lies here:

const reducer = combineReducers(Object.assign({}, rootReducer, {
  routing: routeReducer
}));

rootReducer is not a plain object, but a function that is returned by the method combineReducer, looking like that:

function combination() {
    var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
    var action = arguments[1]; ...
}

So what you do is merging an object with a function, which returns the object only. In your case 'routing' only.

1 Comment

Thank you too @larrydahooster ! Removing the first call to combineReducers (in reducers/index.js) worked ! Maybe if I had wrote combineReducers({rootReducer, routing:routeReducer}) .... ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.