我正在使用 redux 制作下一个应用程序,我可以使用 redux 轻松管理状态,但我想在每次刷新页面后保留 redux 状态,我是 redux 的新手,并且使 redux 状态持久化对我来说变得很棘手。< /p>
_app.js。根页面/组件?
import { Provider } from 'react-redux'
import { store } from '../app/store'
import '../styles/globals.css'
const MyApp = ({ Component, pageProps }) => {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}
export default MyApp
store.js ?
import { configureStore } from "@reduxjs/toolkit";
import basketReducer from "../slices/basketSlice";
import userSlice from "../slices/userSlice";
export const store = configureStore({
reducer: {
users: userSlice,
},
});
userSlice.js ?
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
user: null,
};
export const userSlice = createSlice({
name: "users",
initialState,
reducers: {
addUser: (state, action) => {},
removeUser: (state, action) => {},
},
});
export const { addUser, removeUser } = userSlice.actions;
// Selectors - This is how we pull information from the Global store slice
export const selectUser = (state) => state.users.user;
export default userSlice.reducer;
这是我用于 redux 的代码,但是如果有人通过更改此代码来使我的 redux 状态持久化,这将是一个很大的进步
如果无法更改此代码,任何提示都会有所帮助