-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseApiFetch.js
More file actions
44 lines (37 loc) · 1.18 KB
/
Copy pathuseApiFetch.js
File metadata and controls
44 lines (37 loc) · 1.18 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
44
import { useState, useEffect } from "react";
import axios from "axios";
const useApiFetch = (endpoint) => {
const [data, setData] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const options = {
method: 'GET',
url: 'https://cricbuzz-cricket.p.rapidapi.com/series/v1/international',
headers: {
'X-RapidAPI-Key': "331fa31501msh6fe76b4c14854d4p1fd520jsn7b4d56a08550",
'X-RapidAPI-Host': 'cricbuzz-cricket.p.rapidapi.com'
}
};
const fetchData = async () => {
setIsLoading(true);
try {
const response = await axios.request(options);
setData(response.data);
setIsLoading(false);
} catch (error) {
setError(error);
console.log("useApiFetch ", error);
} finally {
setIsLoading(false);
}
}
useEffect(() => {
fetchData();
}, []);
const refetch = () => {
setIsLoading(true);
fetchData();
}
return { data, isLoading, error, refetch}
}
export default useApiFetch;