MCPcopy Create free account
hub / github.com/Okabe-Rintarou-0/BookStore-Frontend / useData

Function useData

advanced_tutorial/src/cases/case2.tsx:4–29  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

2import { useEffect, useState } from "react";
3
4function 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
31const columns = [{
32 title: "id",

Callers 1

Case2Function · 0.85

Calls 1

mutateFunction · 0.85

Tested by

no test coverage detected