mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-18 19:44:06 -08:00
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 };
|
||
|
};
|