如何在配置存储中使用两个 API reducer 进行 RTK 查询?
How to use two API reducer with RTK query in configuration Store?
我尝试合并两个RTK reducer,但是每次尝试使用concat将其包含在中间件中时,都会出现错误,这是我的代码
store.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;
报告的错误如下图,我不知道到底是什么错误。
我使用多个 API 的方式有问题吗?因为官方文档中没有combine版本
我假设 cartAPI
和 AuthAPI
的 reducerPath
都是 query
,这会导致冲突。你需要给他们不同的 reducerPaths
.
也就是说,强烈建议在整个应用程序中不要有超过一个 api,除非那些查询真正独立的数据,没有重叠。 (如果查询 google 和 twitter 就是这种情况,因为它们是两个独立的公司,但如果查询 Posts 和 Comments 就不是这种情况,因为它们可能相互关联)。
我尝试合并两个RTK reducer,但是每次尝试使用concat将其包含在中间件中时,都会出现错误,这是我的代码
store.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;
报告的错误如下图,我不知道到底是什么错误。
我使用多个 API 的方式有问题吗?因为官方文档中没有combine版本
我假设 cartAPI
和 AuthAPI
的 reducerPath
都是 query
,这会导致冲突。你需要给他们不同的 reducerPaths
.
也就是说,强烈建议在整个应用程序中不要有超过一个 api,除非那些查询真正独立的数据,没有重叠。 (如果查询 google 和 twitter 就是这种情况,因为它们是两个独立的公司,但如果查询 Posts 和 Comments 就不是这种情况,因为它们可能相互关联)。