赞
踩
npm i redux redux-persist
import {configureStore} from "@reduxjs/toolkit";
import {userInfoReduce} from "./userInfoSlice.jsx";
import {globalReducer} from "./globalSlice.jsx";
import {combineReducers} from "redux";
import {persistReducer, persistStore} from 'redux-persist';
import storageSession from 'redux-persist/lib/storage/session'; //sessionStorage
//import storageLocation from 'redux-persist/lib/storage'; //存储到localStorage
const persistConfig = {
key:'root',
storage:storageSession, //指定存储到session中
}
const persistedReducer = persistReducer(
persistConfig,
combineReducers({
//数据切片
userInfo:userInfoReduce,
globalX:globalReducer
})
)
export const store = configureStore({
reducer:persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}),
})
export const persistor = persistStore(store)
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import {Provider} from "react-redux";
import {store,persistor} from "./redux/index.jsx";
import {PersistGate} from "redux-persist/integration/react"
ReactDOM.createRoot(document.getElementById('root')).render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App/>
</PersistGate>
</Provider>
)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。