javascript - 无法对未挂载的组件(Axios 请求)执行 React 状态更新?
问题描述
我有一个带有触发handleSubmit函数的提交按钮的登录表单,在这个函数中我正在做我的axios东西
const handleSubmit = (e) => {
e.preventDefault();
//action creator
loginUser(loginData)
}
我的 Redux 动作创建者:
export const loginUser = (loginData) => (dispatch) => {
axios.post('/signin', loginData)
.then(res => {
let FBIdToken = `Bearer ${res.data.token}`;
localStorage.setItem('FBIdToken', FBIdToken);
})
.catch(err => {
dispatch({
type: SET_ERRORS,
payload: err.response ? err.response.data : {}
});
})
}
现在卸载登录组件后,我收到该错误:
index.js:1 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
我知道我的问题是什么,我需要清理我的 axios 请求,如何清理我的 axios 请求?
解决方案
推荐阅读
- python - 比较嵌套列表中的项目(Python)
- python - 训练后的极大损失和错误的成绩单
- reactjs - 在 asp.net 核心中实现可选查询参数的正确方法是什么?
- android - 如何区分 Android Paging 3 中的初始加载和内容更改?
- javascript - Javascript 到 Python - 低/高字节添加
- python - 通过 Scrapy 代理的请求似乎不起作用,我错过了什么吗?
- angular - 捆绑js文件的角度错误路径,而它适用于其他路线
- kong - 如何在 Kong 中设置“自动索引关闭”(禁用可浏览的 Web 目录)
- google-api - GoogleWebAuthorizationBroker 在 Blazor .Net 5.0 中引发 System.ObjectDisposedException 异常
- python-3.x - 数据流 - Apache Beam - 读取 CSV(来自外部来源:互联网)