Redux中间件未定义

时间:2015-09-27 02:52:19

标签: javascript ecmascript-6 flux babeljs redux

运行此代码时,我收到'中间件不是函数'错误。

import 'babel-core/polyfill';
import { thunkMiddleware, Provider } from 'redux-thunk';
import createLogger from 'redux-logger';
import { createStore, applyMiddleware } from 'redux';
import { fetchDistricts, fetchSchools } from './actions.es6.js';
import rootReducer from './reducers.es6.js';
// import App from './components/App.es6.js';

const logger = createLogger({
    level: 'info',
    collapsed: true,
    predicate: (getState, action) => {action.type; }
});


const createStoreWithMiddleware = applyMiddleware(
    thunkMiddleware,
    logger
)(createStore);

const store = createStoreWithMiddleware(rootReducer);

store.dispatch(fetchDistricts('California')).then(state =>
    {
        var districts = store.getState().districtsByState['California'].districts;
        var fetchSchoolsDfds = [];
        for(var i = 0; i < districts.length; i++) {
            fetchSchoolsDfds.push(store.dispatch(fetchSchools(districts[i].id)));
        }
    }
);

let rootElement = document.getElementById('root');

这是在ES6中,我正在使用Babel进行转换。如果你愿意,我可以发布已编译的代码,但它确实很长。

为什么我收到错误?

修改

好的,我跳了进去,看着被转换的js。看起来有这个功能 -

var createStoreWithMiddleware = _redux.applyMiddleware(_reduxThunk.thunkMiddleware, logger)(_redux.createStore);

和_reduxThunk没有thunkMiddleware属性。在我控制台登出_reduxThunk的控制台中,我回到了这个

function thunkMiddleware(_ref) {
  var dispatch = _ref.dispatch;
  var getState = _ref.getState;

  return function (next) {
    return function (action) {
      return typeof action === 'function' ? action(dispatch, getState) : next(action);
    };
  };
}

所以它看起来像_reduxThunk IS thunkMiddleware。我想这是一个巴贝尔错误 - 为什么巴贝尔犯这个错误?

2 个答案:

答案 0 :(得分:5)

确定。这是非常基本和愚蠢的。原来提供商不在'redux-thunk'中并且处于'redux-react'。

这只是一个错误的错误消息。

答案 1 :(得分:0)

applyMiddleware应该从redux导入。

Nodejs

const applyMiddleware = require("redux").applyMiddleware

角度

import { applyMiddleware } from "redux"