aboutsummaryrefslogtreecommitdiff
path: root/src/useFetch.js
blob: d098e2fc7d4da1869af440bd5e94f2d1a1c1816d (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 };
}