Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果用户未登录,则重定向到主页时发生内存泄漏错误_Javascript_Reactjs_Asynchronous_Axios - Fatal编程技术网

Javascript 如果用户未登录,则重定向到主页时发生内存泄漏错误

Javascript 如果用户未登录,则重定向到主页时发生内存泄漏错误,javascript,reactjs,asynchronous,axios,Javascript,Reactjs,Asynchronous,Axios,如果用户未在componentDidMount中登录(状态401),我将尝试重定向到主页 所以我试过了 componentDidMount(){ const requestPatientListApi=async()=>{ 试一试{ const{data}=wait-get(“/patient”,{requester:“admin”}); 返回数据项; }捕捉(错误){ //当用户未登录时,重定向到“/” 道具.历史.推送(“./”); } 返回[]; }; 如果(!axios.isCancel

如果用户未在componentDidMount中登录(状态401),我将尝试重定向到主页

所以我试过了

componentDidMount(){
const requestPatientListApi=async()=>{
试一试{
const{data}=wait-get(“/patient”,{requester:“admin”});
返回数据项;
}捕捉(错误){
//当用户未登录时,重定向到“/”
道具.历史.推送(“./”);
}
返回[];
};
如果(!axios.isCancel(“”){
//未取消api调用时更新状态
requestPatientListApi()。然后(患者=>{
病人(病人);
});
}
}
组件将卸载(){
如果(取消){
取消();
}
}
但是,错误发生在:

Warning: Can't perform a React state update on an unmounted component. 
我尝试过使用axios cancel令牌,但它似乎不是这种情况的解决方案。
有什么想法吗?

问题是您在设置状态之前正在重定向,在这种情况下,组件将不再呈现。一种方法是转发异步func中的错误,稍后再捕获它

componentDidMount() {
  const requestPatientListApi = async () => {
    try {
      const { data } = await get <AxiosResponse<PatientApi>>("/patient", {
        requester: "admin"
      });
      return data.items;
    } catch (err) {
      throw err; // Forward the error further
    }
    return [];
  };

  if (!axios.isCancel("")) {
    requestPatientListApi().then(patients => {
      setPatients(patients);
    }).catch(err => {
      props.history.push("/"); // handle the error here
    });
  }
}
componentDidMount(){
const requestPatientListApi=async()=>{
试一试{
const{data}=wait get(“/patient”{
请求者:“管理员”
});
返回数据项;
}捕捉(错误){
throw err;//进一步转发错误
}
返回[];
};
如果(!axios.isCancel(“”){
requestPatientListApi()。然后(患者=>{
病人(病人);
}).catch(错误=>{
props.history.push(“/”;//在此处处理错误
});
}
}

问题在于,您在设置状态之前正在重定向,在这种情况下,组件将不再呈现。一种方法是转发异步func中的错误,稍后再捕获它

componentDidMount() {
  const requestPatientListApi = async () => {
    try {
      const { data } = await get <AxiosResponse<PatientApi>>("/patient", {
        requester: "admin"
      });
      return data.items;
    } catch (err) {
      throw err; // Forward the error further
    }
    return [];
  };

  if (!axios.isCancel("")) {
    requestPatientListApi().then(patients => {
      setPatients(patients);
    }).catch(err => {
      props.history.push("/"); // handle the error here
    });
  }
}
componentDidMount(){
const requestPatientListApi=async()=>{
试一试{
const{data}=wait get(“/patient”{
请求者:“管理员”
});
返回数据项;
}捕捉(错误){
throw err;//进一步转发错误
}
返回[];
};
如果(!axios.isCancel(“”){
requestPatientListApi()。然后(患者=>{
病人(病人);
}).catch(错误=>{
props.history.push(“/”;//在此处处理错误
});
}
}