aboutsummaryrefslogtreecommitdiff
path: root/src/useFetch.js
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2022-07-11 17:10:23 -0400
committerGalen Guyer <galen@galenguyer.com>2022-07-11 17:10:23 -0400
commit935ef7d2f76cca7bc1f9840dc357de85f93a1ec2 (patch)
treecb8ee298a747d6084107266e5d2882e60a6b5daa /src/useFetch.js
parent3694bf061ecfe0f6733a0cb9028d044dfc8cf9d2 (diff)
Update for new dashboard frontend
Diffstat (limited to 'src/useFetch.js')
-rw-r--r--src/useFetch.js17
1 files changed, 17 insertions, 0 deletions
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 };
+}