使用react-redux - configureStore时输入错误

wvt8vs2t  于 2023-06-06  发布在  React
关注(0)|答案(1)|浏览(237)

我有以下程序代码。当运行应用程序时,我得到一个TypeError,并标记了configureStore。我做错了什么?
TypeError:baseReducer不是函数

import { combineReducers, configureStore } from "@reduxjs/toolkit";
import userReducer from "./userSlice";
import darkModeSlice from "./darkModeSlice";
import {
 persistStore,
 persistReducer,
 FLUSH,
 REHYDRATE,
 PAUSE,
 PERSIST,
 PURGE,
 REGISTER,
} from "redux-persist";

import storage from "redux-persist/lib/storage";

const persistConfig = {
 key: "root",
 version: 1,
 storage,
};

const rootReducer = combineReducers({
 user: userReducer,
 mode: darkModeSlice,
});

const persistedReducer = persistReducer(persistConfig, { rootReducer });

export const store = configureStore({
 reducer: persistedReducer,
 middleware: (getDefaultMiddleware) =>
   getDefaultMiddleware({
     serializableCheck: {
       ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
    },
   }),
 });

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export const persistor = persistStore(store);
2eafrhcq

2eafrhcq1#

此问题是由将rootReducer传递给persistReducer函数的方式引起的。应该直接传递rootReducer,而不是将其 Package 在对象中。更改以下行:

const persistedReducer = persistReducer(persistConfig, { rootReducer });

致:

const persistedReducer = persistReducer(persistConfig, rootReducer);

相关问题