-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathuseFetch.js
More file actions
27 lines (22 loc) · 609 Bytes
/
Copy pathuseFetch.js
File metadata and controls
27 lines (22 loc) · 609 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { useState, useEffect } from "react";
const useFetch = url => {
const [error, setError] = useState();
const [loading, setLoading] = useState(false);
const [response, setResponse] = useState();
useEffect(() => {
const fetchInfo = async () => {
setLoading(true);
try {
const response = await fetch(url);
const result = await response.json();
setResponse(result);
} catch (error) {
setError(error);
}
setLoading(false);
};
fetchInfo();
}, [url]);
return { error, loading, response };
};
export default useFetch;