我正在使用 React 构建一个网页。我使用了 udemy 的教程,其中作者使用 redux Promise 进行剩余调用。它在类(class)中发挥了作用。
在我自己的项目中,我设置了相同的所有内容,当我尝试使用 redux-promise 中间件创建商店时,启动网站时出现以下错误:
Uncaught TypeError: middleware is not a function
at http://localhost:3000/index.js:34412:16
at Array.map (native)
at http://localhost:3000/index.js:34411:27
at Object.<anonymous> (http://localhost:3000/index.js:15744:70)
at Object.<anonymous> (http://localhost:3000/index.js:15748:27)
at Object.options.path (http://localhost:3000/index.js:15749:30)
at __webpack_require__ (http://localhost:3000/index.js:658:30)
at fn (http://localhost:3000/index.js:86:20)
at Object.<anonymous> (http://localhost:3000/index.js:35118:18)
at __webpack_require__ (http://localhost:3000/index.js:658:30)
这里出现错误(根据控制台):
applyMiddleware.js:39
如果我使用另一个中间件,比如 redux-thunk,它就可以工作(好吧,除了我的其余调用,但那是另一个故事了)。有什么建议为什么会发生这个错误吗?
我正在运行的版本:
"axios": "^0.15.3",
"es6-promise": "^4.0.5",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^5.0.4",
"react-router": "^3.0.2",
"redux": "^3.6.0",
"redux-promise": "^0.5.3"
我的index.tsx:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import {Router, Route, browserHistory, IndexRoute} from 'react-router';
import thunk from 'redux-thunk';
import reduxPromise from 'redux-promise';
import reducers from './app/reducers';
import routes from './routes';
import './index.scss';
const createStoreWithMiddleware = applyMiddleware(reduxPromise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<Router history={browserHistory} routes={routes} />
</Provider>
,document.getElementById('root')
);
您需要更多信息(如 reducer 、操作等)吗?
当调用此行时,会出现错误:
<Provider store={createStoreWithMiddleware(reducers)}>
最佳答案
applymiddleware 是一个存储增强器,它作为参数传入以创建存储。
让代码运行的最简单方法是替换以下内容:
const createStoreWithMiddleware = applyMiddleware(reduxPromise)(createStore);
至:
const middleware = [ reduxPromise ];
const store = createStore(reducer, applyMiddleware(...middleware));
如您所见,您必须将根 reducer 作为 createStore 的第一个参数传递。
关于reactjs - redux-promise:未捕获类型错误:中间件不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649902/