【问题标题】:Pervious state is removed when I update state in react native redux当我在反应本机 redux 中更新状态时,会删除以前的状态
【发布时间】:2021-09-24 11:57:33
【问题描述】:

登录后我在应用程序内调用 get_user_profile_api。成功登录后,我的应用程序的状态为用户和令牌。但是一旦调用了 GET_USER_PROFILE_SUCCESS,状态就只剩下 userData 和 isLoading。用户和令牌从状态中移除。

import { CONST } from '../../utils/Const';
import * as types from './types';

const initialState = {
  isLoading: false,
  user: null,
  userData: null,
  isLoggedIn: false,
  token: '',
};

export default authReducer = (state = initialState, action) => {
  console.log('action :: ', action);
  switch (action.type) {
    case types.GET_USER_PROFILE_LOADING:
    case types.LOGIN_LOADING:
      return {
        isLoading: true,
      };

    case types.GET_USER_PROFILE_SUCCESS:
      return {
        ...state,
        isLoading: false,
        userData: action.payload,
      };

    case types.LOGIN_SUCCESS:
      return {
        ...state,
        isLoading: false,
        isLoggedIn: true,
        user: action.payload.user,
        token: action.payload.token,
      };

    case types.LOGIN_ERROR:
    case types.GET_USER_PROFILE_ERROR:
      return {
        ...state,
        isLoading: false,
      };

    default:
      return {
        ...state,
        isLoading: false,
      };
  }
};

【问题讨论】:

    标签: javascript react-native react-redux


    【解决方案1】:

    这段代码没有错正确道具

    【讨论】:

      【解决方案2】:

      我没有在 LOADING 案例中写 ...state。这就是我的状态被删除的原因。

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多