用户 authReduce 时出现错误。当记录这个减速器并显示“underfine”时,我正在使用:
- Redux 持久存储身份验证状态
- Redux 工具包
用户 authReduce 时出现错误。当记录这个减速器并显示“underfine”时,我正在使用:
当我将减速器和动作存储在同一个文件中时出现问题(在一个文件中有太多导出)所以我通过拆分减速器(authReducer.js)和动作(authSlice.js)是这样的两个文件来解决这个问题:
// authReducer.js
import { createSlice } from '@reduxjs/toolkit';
export const authSlice = createSlice({
name: 'auth',
initialState: {
isLoading: true,
userToken: null,
isUserGuest: true,
isJailBroken: false,
isSubscription: true,
isShowCheckPassCode: false,
passCodeMessage: 'Đây là phiên bản nội bộ, Vui lòng nhập pass để sử dụng.',
passCode: '',
isShowPopupPassword: false,
userEmail:'',
typeLogin:'',
isShowLoginGGFB:true
},
reducers: {
signIn: {
reducer(state, action) {
const { token } = action.payload;
state.userToken = token
state.isLoading = false
},
prepare(token, userInfo) {
return { payload: { token, userInfo } }
}
},
skipAuth(state, action) {
state.isUserGuest = true;
state.userToken = null;
},
signout(state, action) {
state.userToken = null
state.isUserGuest = false;
},
setIsJailBroken(state, action) {
state.isJailBroken = true
},
setSubscriptionNoti(state, action) {
state.isSubscription = action.payload
},
setIsShowCheckPassCode(state, action) {
state.isShowCheckPassCode = action.payload
},
setDataPasscode(state, action) {
state.isShowCheckPassCode = action.payload.isShowCheckPassCode
state.passCodeMessage = action.payload.passCodeMessage
state.passCode = action.payload.passCode
},
setShowLoginGGFB(state, action){
state.isShowLoginGGFB = action.payload
},
setActivePopupPassword(state, action) {
state.isShowPopupPassword = action.payload
},
setUserEmail(state,action){
state.userEmail = action.payload
},
setTypeLogin(state,action){
state.typeLogin = action.payload
}
}
})
export const authReducer = authSlice.reducer
// authSlice.js
export const {
signIn,
signout,
restore,
skipAuth,
setIsJailBroken,
setSubscriptionNoti,
setIsShowCheckPassCode,
setDataPasscode,
setActivePopupPassword,
setUserEmail,
setTypeLogin,
setShowLoginGGFB,
} = authSlice.actions;
export const callActionSignOut = (isForce = true) => async dispatch => {
try {
if (isForce) {
await signOut();
}
dispatch(signout());
dispatch(
setFavorite({
dataFavorite: [],
objectDataFavorite: {},
}),
);
dispatch(fetchCartSuccess(null));
dispatch(resetUserInternal());
dispatch(clearUser());
} catch (error) {
console.log('callActionSignOut -> error', error);
}
};