Kai*_*195 5 reactjs react-redux rtk-query
我尝试组合两个RTK减速器,但是每次尝试使用concat将其包含在中间件中时,都会出现错误,这是我的代码
商店.ts
import { configureStore,combineReducers } from "@reduxjs/toolkit";
import cartReducer from "./cartRedux";
import userReducer from "./authRedux";
import { AuthAPI } from "./authRTK";
import { cartAPI } from "./cartRTK";
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from "redux-persist";
import storage from 'redux-persist/lib/storage'
const persistConfig = {
key: "root",
blacklist:['AuthAPI','cartAPI'],
storage,
};
const rootReducer = combineReducers({
[cartAPI.reducerPath]: cartAPI.reducer,
[AuthAPI.reducerPath]: AuthAPI.reducer,
auth:userReducer,
cart:cartReducer,
})
const persistedReducer = persistReducer(persistConfig, rootReducer);
export const store = configureStore({
reducer: persistedReducer,
// reducer: {
// cart: cartReducer,
// auth: persistedReducer,
// [api.reducerPath]: api.reducer,
// },
middleware: (gDM) =>
gDM({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}).concat([AuthAPI.middleware,cartAPI.middleware]), //getDefaultMiddleware
});
export let persistor = persistStore(store);
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
Run Code Online (Sandbox Code Playgroud)
报告的错误如下图所示,我无法弄清楚错误到底是什么。
我使用多个 API 的方式有问题吗?因为官方文档中没有combine版本。
我假设cartAPI和AuthAPI都有一个reducerPathof query,导致冲突。你需要给他们不同的reducerPaths。
也就是说,强烈建议您的整个应用程序中不要使用多个 api,除非这些 api 查询真正独立的数据且没有重叠。(如果查询 google 和 twitter 就是这种情况,因为它们是两家独立的公司,但如果查询帖子和评论则不会,因为它们可能是链接的)。
| 归档时间: |
|
| 查看次数: |
7646 次 |
| 最近记录: |