0

我正在尝试在我的通用应用程序中实现 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;

问题是我得到了这个错误:期望减速器是一个函数。我在 StackOverflow 上搜索并搜索过(那里有一些类似的问题),但答案在我的情况下不起作用。

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

谢谢。

4

2 回答 2

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);
于 2016-05-05T12:32:46.787 回答
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;
于 2016-05-05T10:05:44.320 回答