安装依赖:
npm install redux-persist
改造 src/store/index.js
javascript">import { configureStore } from '@reduxjs/toolkit'
import userSlice from './userSlice'
import storage from 'redux-persist/lib/storage';
import { persistReducer, persistStore } from 'redux-persist';
import { combineReducers } from 'redux';const rootReducer = combineReducers({userSlice
});const persistConfig = {key: 'root',storage,blacklist: [],
};const persistedReducer = persistReducer(persistConfig, rootReducer);const store = configureStore({reducer: persistedReducer,middleware: (getDefaultMiddleware) => getDefaultMiddleware({serializableCheck: false})
});export const persistor = persistStore(store);
export default store
注意:一定要配置下图中这个,不然会用不了
不然就会报以下错误: