-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathuseFetch.jsx
More file actions
26 lines (19 loc) · 737 Bytes
/
Copy pathuseFetch.jsx
File metadata and controls
26 lines (19 loc) · 737 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
import { useEffect, useState } from "react";
const APIKEY = import.meta.env.VITE_GIPHY_API;
const useFetch = ({ keyword }) => {
const [gifUrl, setGifUrl] = useState("");
const fetchGifs = async () => {
try {
const response = await fetch(`https://api.giphy.com/v1/gifs/search?api_key=${APIKEY}&q=${keyword.split(" ").join(" ")}&limit=1`);
const { data } = await response.json();
setGifUrl(data[0]?.images?.downsized_medium.url);
} catch (error) {
setGifUrl("https://metro.co.uk/wp-content/uploads/2015/05/pokemon_crying.gif?quality=90&strip=all&zoom=1&resize=500%2C284");
}
};
useEffect(() => {
if (keyword) fetchGifs();
}, [keyword]);
return gifUrl;
};
export default useFetch;