Answers for "Fetching data error and pending load check"

0

Fetching data error and pending load check

import { useState, useEffect } from "react";

const useFetch = (url) => {
  const [data, setData] = useState(null);
  const [isPending, setIsPending] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch(url)
      .then((res) => {
        console.log(res);
        if (!res.ok) {
          throw Error("could not fetch data for that resource");
        }
        return res.json();
      })
      .then((data) => {
        setData(data);
        setIsPending(false);
        setError(null);
      })
      .catch((err) => {
        setIsPending(false);
        setError(err.message);
      });
  }, []);
  return { data, isPending, error };
};

export default useFetch;
Posted by: Guest on October-07-2021

Code answers related to "Fetching data error and pending load check"

Browse Popular Code Answers by Language