| 2 | import { useEffect, useState } from "react"; |
| 3 | |
| 4 | function useData<T>(url: string) { |
| 5 | const [loading, setLoading] = useState<boolean>(false); |
| 6 | const [data, setData] = useState<T | undefined>(); |
| 7 | const [error, setError] = useState<Error | undefined>(); |
| 8 | const mutate = async () => { |
| 9 | setLoading(true); |
| 10 | try { |
| 11 | const response = await fetch(url, { method: "GET" }); |
| 12 | if (!response.ok) { |
| 13 | const error = Error(`HTTP error! status: ${response.status}`); |
| 14 | setError(error); |
| 15 | return; |
| 16 | } |
| 17 | const data = await response.json() as T; |
| 18 | setData(data); |
| 19 | } catch (error) { |
| 20 | setError(error as Error); |
| 21 | } finally { |
| 22 | setLoading(false); |
| 23 | } |
| 24 | } |
| 25 | useEffect(() => { |
| 26 | mutate(); |
| 27 | }, [url]); |
| 28 | return { data, loading, error, mutate } |
| 29 | } |
| 30 | |
| 31 | const columns = [{ |
| 32 | title: "id", |