score:1

    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;

Related Query

More Query from same tag