-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDataFetch.jsx
More file actions
43 lines (36 loc) · 1 KB
/
Copy pathuseDataFetch.jsx
File metadata and controls
43 lines (36 loc) · 1 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable react-hooks/rules-of-hooks */
import { useEffect, useState } from "react";
export default function useDataFetch(url, options) {
const opt = options ? options : {}
const [responseData, setResponseData] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setLoading] = useState(false);
const fetchData = async () => {
setLoading(true);
try {
const response = await fetch(url, {
...opt,
method:"GET",
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json',
...opt?.headers,
}
});
const data = await response.json();
setResponseData(data);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, [url, options]);
const refetch = () => {
fetchData();
};
return { responseData, error, isLoading, refetch };
}