react redux添加“redux-thunk”中间件来存储

时间:2018-02-05 07:05:22

标签: reactjs redux react-redux

我想将redux-thunk添加到商店,但是当在商店文件中添加此middlewares.push(thunkMiddleware)时,我在控制台中收到错误middleware is not a function。我怎么能解决这个问题?

我的'store.js'文件(第21行):

import { createStore, applyMiddleware, compose } from "redux";
import { browserHistory } from "react-router";
import { syncHistoryWithStore, routerMiddleware } from "react-router-redux";
import createSagaMiddleware from "redux-saga";
import {thunk, thunkMiddleware } from 'redux-thunk';
import freeze from "redux-freeze";
import { reducers } from "./reducers/index";
import { sagas } from "./sagas/index";

// add the middlewares
let middlewares = [];

// add the router middleware
middlewares.push(routerMiddleware(browserHistory));

// add the saga middleware
const sagaMiddleware = createSagaMiddleware();
middlewares.push(sagaMiddleware);

// add redux-thunk middleware
middlewares.push(thunkMiddleware);

// add the freeze dev middleware
if (process.env.NODE_ENV !== 'production') {
  middlewares.push(freeze);
}

// apply the middleware
let middleware = applyMiddleware(...middlewares);

// add the redux dev tools
if (process.env.NODE_ENV !== 'production' && window.devToolsExtension) {
  middleware = compose(middleware, window.devToolsExtension());
}

// create the store
const store = createStore(reducers, middleware);
const history = syncHistoryWithStore(browserHistory, store);
sagaMiddleware.run(sagas);

// export
export { store, history };

错误: Uncaught TypeError: middleware is not a function

1 个答案:

答案 0 :(得分:2)

thunk是来自default export的{​​{1}},您需要将其用作中间件,而不是redux-thunk

thunkmiddleware