当调用 createStore() 我得到: TypeError: middleware is not a function

Posted

技术标签:

【中文标题】当调用 createStore() 我得到: TypeError: middleware is not a function【英文标题】:When calling createStore() I get: TypeError: middleware is not a function 【发布时间】:2019-11-13 12:18:16 【问题描述】:

在标记此内容之前,请注意我已检查过的重复项:

How to dispatch a Redux action with a timeout? TypeError: middleware is not a function × TypeError: middleware is not a function

我正在尝试在我的 Redux 商店中集成 redux-thunk 并同时使用替换 replaceReducer

基本上,我有一个地方:

const createStore, applyMiddleware = require('redux');
const thunk = require('redux-thunk');
createStore(function() return , applyMiddleware(thunk));
// also tried
// createStore(function() return , , applyMiddleware(thunk));

后来:

store.replaceReducer(someCombinedReducer);

现在,我收到一个通过 createStore() 行触发的错误(所以在更换任何减速器之前)。

TypeError: middleware is not a function

版本:

redux:4.0.1 redux-thunk:2.3.0

编辑:

堆栈跟踪指向applyMiddleware 函数,正如我直接从我进行的调用中的问题TypeError: middleware is not a function 一样。

【问题讨论】:

您能否详细说明该错误的来源?这是实际的运行时错误吗?打字稿错误?有堆栈跟踪吗? @markerikson 已编辑 【参考方案1】:

经过一夜好眠和一些调整。

// thunk here is not undefined but and object
const thunk = require('redux-thunk');

应替换为:

const thunk = require('redux-thunk').default;

【讨论】:

以上是关于当调用 createStore() 我得到: TypeError: middleware is not a function的主要内容,如果未能解决你的问题,请参考以下文章