score:1

you need to make sure the component is still mounted before trying to update the state:

    import { usestate, useeffect } from "react";
import axios from "axios";

const usefetch = (url) => {
      const [data, setdata] = usestate(null);
      const [error, seterror] = usestate(null);
      const ismounted=useref(null)
      useeffect(()=>{ ismounted.current=true; return()=>{ismounted.current=false}},[])
      useeffect(() => {
        axios
          .get(url)
          .then((response) => {
            if (ismounted.current)
            {setdata(response.data);}
          })
          .catch((err) => {
            if (ismounted.current)
            {seterror(err);}
          });
      }, [url]);
    
      return { data, error };
    };
    
    export default usefetch;

set a mutable value to true right after the component mounts, and set it to false when it is going to unmount.

before every setstate check if it is still mounted.


Related Query

More Query from same tag