4

我正在尝试根据README 中的步骤设置 connected-react-router 。

我的 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;

我尝试按照设置进行操作,但Uncaught TypeError: rootReducer is not a function浏览器中不断出现错误。

目前该文件如下所示:

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;
4

1 回答 1

2

给 connectRouterhistory代替 reducer。所以:

connectRouter(history)(reducer),

而不是connectRouter(reducer)(history).

您说的是import reducer from './reducers';指包含减速器的文件夹。假设 reducers 文件夹有三个文件(index.js、reducer1.js 和 reducer2.js),那么这样的东西reducers应该可以工作:

索引.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;
于 2018-08-24T06:51:56.323 回答