React + Redux - 期望reducer成为一个函数

时间:2016-05-05 09:40:52

标签: javascript reactjs redux react-router-redux

我试图在我的通用应用中实现redux,但我在redux上遇到了一些问题。 我有这个configureStore函数:

import { createStore, applyMiddleware, compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger'
import rootReducer from '../reducers/index';
import { routerMiddleware } from 'react-router-redux';

export default function configureStore(history, initialState) {
  const reduxRouterMiddleware = routerMiddleware(history);
  let finalCreateStore;

  if (__DEVELOPMENT__ && __CLIENT__ && __DEVTOOLS__) {
    const { persistState } = require('redux-devtools');
    const DevTools = require('../containers/DevTools/DevTools');

    finalCreateStore = compose(
      applyMiddleware(thunkMiddleware)(createStore),
      window.devToolsExtension ? window.devToolsExtension() : DevTools.instrument(),
      persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
    )(createStore);
  }
  else {
    finalCreateStore = applyMiddleware(thunkMiddleware)(createStore);
  }

  const store = finalCreateStore(rootReducer, initialState);

  if (__DEVELOPMENT__ && module.hot) {
    module.hot.accept(rootReducer, () => {
      store.replaceReducer(rootReducer);
    });
  }

  return store;
}

然后我的rootReducer文件看起来像这样:

import { combineReducers } from 'redux';
import environment from './environment';
import general from './general';
import alert from './alert';
import user from './user';
import { routerReducer } from 'react-router-redux'


const rootReducer = combineReducers({
  environment,
  general,
  alert,
  user,
  routing: routerReducer
});

export default rootReducer;

问题是我得到了这个错误:期望reducer成为一个函数。 我已经在StackOverflow上搜索并搜索了(其中存在类似的问题),但答案在我的案例中并不起作用。

那么,我做错了什么?为什么?

感谢。

2 个答案:

答案 0 :(得分:2)

试试这个:

finalCreateStore = compose(
    // you write more than one createStore here
    applyMiddleware(thunkMiddleware),
    window.devToolsExtension ? window.devToolsExtension() : DevTools.instrument(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
)(createStore);

答案 1 :(得分:0)

你试过吗

import { combineReducers } from 'redux';
import environment from './environment';
import general from './general';
import alert from './alert';
import user from './user'; 
import { routerReducer } from 'react-router-redux'


export const rootReducer = combineReducers({
  environment: environment,
  general: general,
  alert: alert,
  user: user,
 routing: routerReducer
});

export default rootReducer;