我正在用reactjs开发一个应用程序,并使用redux作为我的状态管理工具。
我想使用redux-persistent来持久化我从服务器获取的数据
但是我收到这个错误
Uncaught TypeError: getDefaultMiddleware is not a function
这是我的代码:
import { configureStore, combineReducers } from '@reduxjs/toolkit'
import userReducer from './UserSlice'
import storage from 'redux-persist/lib/storage'
store.js
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist'
const rootReducer = combineReducers({user: userReducer})
This is where I create the persist config
const persistConfigs = {
key: 'root',
version: 1,
storage,
}
这是我使用持久配置的地方
const persistedReducer = persistReducer(persistConfigs, rootReducer)
export const store = configureStore({
reducer: {
reducer: persistReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
},
})
export const persistor = persistStore(store)
我认为应该是:
export const store = configureStore({
reducer: persistReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
})
您将所有选项嵌套到密钥reducer
中,根据文档,这似乎不是正确的
export const store = configureStore({
reducer: { // <---- Remove this
reducer: persistReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
},
})