Rachel
Rachel

Reputation: 157

Setting Up Connected React Router

I'm trying to set up connected-react-router according to the steps in the README.

I have this current code in my store.js:

import { createStore } from 'redux';
import reducer from './reducers';
import { middleware, runSagas } from './middleware';

const createSWStore = () => {
  const store = createStore(reducer, middleware);
  runSagas();
  return store;
};

export default createSWStore;

I've tried to follow the set up, but I keep getting a Uncaught TypeError: rootReducer is not a function error in the browser.

Currently the file looks like this:

import { createBrowserHistory } from 'history';
import { applyMiddleware, compose, createStore } from 'redux';
import { connectRouter, routerMiddleware } from 'connected-react-router';

import reducer from './reducers';
import { middleware, runSagas } from './middleware';

const history = createBrowserHistory();

const createSWStore = () => {
  const store = createStore(
    connectRouter(reducer)(history),
    compose(
      applyMiddleware(
        routerMiddleware(history),
      ),
    ),
    middleware);
  runSagas();
  return store;
};

export default createSWStore;

Upvotes: 4

Views: 4827

Answers (1)

Lauri Harpf
Lauri Harpf

Reputation: 1488

Give connectRouter the history instead of the reducer. So:

connectRouter(history)(reducer),

instead of connectRouter(reducer)(history).

You said that import reducer from './reducers'; refers to a folder containing the reducers. Assuming that the reducers folder has three files (index.js, reducer1.js and reducer2.js), then having something like this in reducers should work:

Index.js

import reducer1 from './reducer1';
import reducer2 from './reducer2';
import { combineReducers } from 'redux';

export default combineReducers({ reducer1, reducer2 });

reducer1.js

const reducer1 = (state = { myState: "foo" }, action) => {
    return state;
};

export default reducer1;

reducer2.js

const reducer2 = (state = { myState: "bar" }, action) => {
    return state;
};

export default reducer2;

Upvotes: 2

Related Questions