aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/useFetch.js
blob: a4eaa746ddd1a0affed9d32a57fb548c42bb0025 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { useEffect, useState } from "react";

export default function useFetch(url, options) {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch(url, options)
      .then((resp) => resp.json())
      .then((resp) => setData(resp))
      .catch((err) => setError(err))
      .finally(() => setLoading(false));
  }, []);

  return { data, loading, error };
}