Redux Persist
基本用法
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import logger from "redux-logger";
import usersReducer from "@/pages/users/usersSlice";
import postsReducer from "@/pages/posts/postsSlice";
import notificationsReducer from "@/pages/notifications/notificationsSlice";
import {persistStore, persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER,} from "redux-persist"
import storage from "redux-persist/lib/storage"; // 默认为 localStorage
const isDev = import.meta.env.DEV;
export const rootReducer = combineReducers({
users: usersReducer,
posts: postsReducer,
notifications: notificationsReducer,
});
const persistedReducer = persistReducer({ key: "root", storage }, rootReducer);
const store = configureStore({
reducer: persistedReducer,
devTools: isDev,
middleware: getDefaultMiddleware => isDev ? getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
}
}).concat(logger) : getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
}
}),
})
export const persistor = persistStore(store);
export default store;API
persistReducer(config, reducer)
persistStore(store, [config, callback])
persistor
黑名单和白名单
最后更新于