Auth.js
import { createSlice } from "@reduxjs/toolkit";
export const authSlice = createSlice({
name: "auth",
initialState: {
isAuthenticated: false,
user: {},
},
reducers: {
setUser: (state, { payload }) => {
state.user = payload.user;
state.isAuthenticated = true;
},
logout: (state) => {
state.user = {};
state.isAuthenticated = false;
},
},
});
export const { setUser, logout } = authSlice.actions;
export default authSlice.reducer;
index.js(存储)
import { configureStore } from "@reduxjs/toolkit";
import authReducer from "./auth.js";
export default configureStore({
reducer: {
auth: authReducer,
},
});
交易表单. jsconst { categories } = useSelector((state) => state.auth.user);
当我登录时出现错误,但重新加载页面时错误消失。如何修复错误
1条答案
按热度按时间zengzsys1#
如果你没有登录,你就得不到用户,你就不能从中提取类别。
根据您对用户和类别数据所做的操作,您最好执行以下操作: