首页 > 解决方案 > 无法对未挂载的组件(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 请求?

标签: javascriptreactjsaxioscode-cleanup

解决方案


推荐阅读