prometheus/web/ui/react-app/src/utils/useFetch.ts
Chris Marchbanks f17a0e17aa Implement the /flags page in react (#6248)
* 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>
2019-11-02 10:27:36 +01:00

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 };
};