mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 14:17:12 +02:00
* Implement the /flags page in react Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com> * Use custom react hook for calling api Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
28 lines
707 B
TypeScript
28 lines
707 B
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
export const useFetch = (url: string, options?: RequestInit) => {
|
|
const [response, setResponse] = useState();
|
|
const [error, setError] = useState();
|
|
const [isLoading, setIsLoading] = useState();
|
|
|
|
useEffect(() => {
|
|
const fetchData = async () => {
|
|
setIsLoading(true);
|
|
try {
|
|
const res = await fetch(url, options);
|
|
if (!res.ok) {
|
|
throw new Error(res.statusText);
|
|
}
|
|
const json = await res.json();
|
|
setResponse(json);
|
|
setIsLoading(false);
|
|
} catch (error) {
|
|
setError(error);
|
|
}
|
|
};
|
|
fetchData();
|
|
}, [url, options]);
|
|
|
|
return { response, error, isLoading };
|
|
};
|