【问题标题】:No reducer provided for key reducer (reducer underfine)没有为 key reducer 提供 reducer (reducer undefined)
【发布时间】:2020-05-04 07:57:28
【问题描述】:

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

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

有时我尝试热重载它工作。所以我不明白为什么?

【问题讨论】:

  • 您确定要导入声明吗?来自@features/auth/authSlice
  • 是的,我使用 babel-plugin-module-resolver 和 decorator 来写 alias import
  • 我认为原因是我在 authSlice 文件中导出了太多函数。该文件包括 authSlice、authAction、authReducer 和 Midderware(thunk) 的许多功能操作。所以我尝试为 Reducer 写一个文件它工作了
  • 您能否详细说明您为解决我在使用 redux-toolkig 和 redux-persist 时遇到的问题所做的工作
  • 嗨@AleksandrFomin 我在下面回答这个问题。希望对你有帮助

标签: react-native redux-toolkit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2019-09-05
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多