0

用户 authReduce 时出现错误。当记录这个减速器并显示“underfine”时,我正在使用:

  • Redux 持久存储身份验证状态
  • Redux 工具包

有时我尝试热重载它工作。所以我不明白为什么? 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

0

当我将减速器和动作存储在同一个文件中时出现问题(在一个文件中有太多导出)所以我通过拆分减速器(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);
  }
};

于 2020-08-11T03:23:43.937 回答