gpt4 book ai didi

javascript - 类型错误 : middleware is not a function in store

转载 作者:行者123 更新时间:2023-11-29 10:01:43 25 4
gpt4 key购买 nike

我有下面的代码,用于在 react-redux 中组合存储和 reducer。
它在我以前的应用程序中完美运行可能是由于 react 和 react-redux 版本。

  • 但现在我已经使用最新版本设置了新的 React 项目,但出现了错误。

TypeError: middleware is not a function

代码:

import 'rxjs'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { reducer as formReducer } from 'redux-form'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import { createEpicMiddleware, combineEpics } from 'redux-observable'
// import { createLogger } from 'redux-logger'
import user, { userEpic } from './user/duck'
import userApp, { userAppEpic } from './user-app/duck'

import app from './app'

// Bundling Epics
const rootEpic = combineEpics(
userEpic,
userAppEpic
)

// Creating Bundled Epic
const epicMiddleware = createEpicMiddleware()

// Define Middleware
const middleware = [
thunk,
promise(),
epicMiddleware
]

// Define Reducers
const reducers = combineReducers({
app,
user,
userApp,
form: formReducer
})

// Create Store
export default createStore(reducers,
applyMiddleware(...middleware))
epicMiddleware.run(rootEpic)

有人可以帮我解决这个问题吗?

最佳答案

根据 Doc :

...middleware (arguments): Functions that conform to the Redux middleware API. Each middleware receives Store's dispatch and getState functions as named arguments, and returns a function.

这意味着我们需要将函数传递给applyMiddleware。在您的情况下,您传递的是 promise() 而不是 promise (函数),这就是它失败并出现错误的原因:

middleware is not a function

这样写:

const middleware = [ thunk, promise, epicMiddleware ];

关于javascript - 类型错误 : middleware is not a function in store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55311749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com