javascript - 当我在反应本机 redux 中更新状态时,会删除以前的状态
问题描述
登录后我在应用程序内调用 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,
};
}
};
解决方案
这段代码没有任何问题,我认为问题在于您从哪里调用这些操作,如果有令牌,或者您没有将状态正确映射到道具,您可以在使用效果挂钩中调用它们以获取用户配置文件
推荐阅读
- python - 括号中的 Python Pandas 正则表达式数字从列到列表
- sql - oracle sql regexp删除字母t
- javascript - 将 Node/JavaScript 中的格式化日期转换为 UTC
- laravel - 防止 laravel 网站受到 bot 攻击
- javascript - 通过在数组 JavaScript 中推送元素来创建一个新的对象数组
- node.js - nodejs - Buffer,File,String,读取和写入编码之间的差异
- node.js - 通过 NestJS 将类实现注入基于接口的抽象控制器实现
- maven - 如何在 Windows 中设置 Maven?
- jquery - 自定义引导表分页
- php - 关键未捕获错误:在 elementor-pro/core/app/modules/site-editor/module.php:80 中调用 null 上的成员函数 register_controller()