我正在尝试向已经使用RTK查询的商店添加一个自定义中间件。但是我得到了“警告: reducerPath上的reducerPath中间件”还没有添加到商店中。运行时出错,尽管我确实添加了api中间件。这是我的store.ts文件:
import { combineReducers, configureStore } from '@reduxjs/toolkit'
import { apiSlice } from './apiSlice'
import { homeModeReducer, workingReducer, bannersReducer, latestEventDataReducer } from './reducers/reducers'
import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux'
import { latestMiddleware } from './middleware'
const reducer = combineReducers({
[apiSlice.reducerPath]: apiSlice.reducer,
mode: homeModeReducer,
working: workingReducer,
banners: bannersReducer,
latestEventData: latestEventDataReducer
})
const store = configureStore({
reducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(latestMiddleware).concat(apiSlice.middleware)
})
export default store
export type RootState = ReturnType<typeof reducer>;
export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector我的中间件被定义为:
import { Middleware } from 'redux'
import { RootState } from './store'
export const latestMiddleware: Middleware<
{}, // Most middleware do not modify the dispatch return value
RootState
> = storeApi => next => action => {
console.log(action.type)
return storeApi.getState() // correctly typed as RootState
}它编译正确,但在运行时失败。有什么帮助吗?
发布于 2022-02-24 09:33:06
您的latestMiddleware没有调用next(action),这意味着下一个中间件(以及最后一个简化器)将永远不会被执行。
https://stackoverflow.com/questions/71249368
复制相似问题