MCPcopy Create free account
hub / github.com/Klerith/react-hooks / useFetch

Function useFetch

src/hooks/useFetch.js:4–42  ·  view source on GitHub ↗
( url )

Source from the content-addressed store, hash-verified

2
3
4export const useFetch = ( url ) => {
5
6 const [state, setState] = useState({
7 data: null,
8 isLoading: true,
9 hasError: null,
10 })
11
12
13 const getFetch = async () => {
14
15 setState({
16 ...state,
17 isLoading: true,
18 });
19
20 const resp = await fetch(url);
21 const data = await resp.json();
22
23 setState({
24 data,
25 isLoading: false,
26 hasError: null,
27 });
28 }
29
30
31 useEffect(() => {
32 getFetch();
33 }, [url])
34
35
36
37 return {
38 data: state.data,
39 isLoading: state.isLoading,
40 hasError: state.hasError,
41 };
42}

Callers 2

MultipleCustomHooksFunction · 0.90
LayoutFunction · 0.90

Calls 1

getFetchFunction · 0.85

Tested by

no test coverage detected