From 935ef7d2f76cca7bc1f9840dc357de85f93a1ec2 Mon Sep 17 00:00:00 2001 From: Galen Guyer Date: Mon, 11 Jul 2022 17:10:23 -0400 Subject: Update for new dashboard frontend --- src/useFetch.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/useFetch.js (limited to 'src/useFetch.js') diff --git a/src/useFetch.js b/src/useFetch.js new file mode 100644 index 0000000..d098e2f --- /dev/null +++ b/src/useFetch.js @@ -0,0 +1,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 }; +} -- cgit v1.2.3