({ keyword })
| 3 | const APIKEY = import.meta.env.VITE_GIPHY_API; |
| 4 | |
| 5 | const useFetch = ({ keyword }) => { |
| 6 | const [gifUrl, setGifUrl] = useState(""); |
| 7 | |
| 8 | const fetchGifs = async () => { |
| 9 | try { |
| 10 | const response = await fetch(`https://api.giphy.com/v1/gifs/search?api_key=${APIKEY}&q=${keyword.split(" ").join("")}&limit=1`); |
| 11 | const { data } = await response.json(); |
| 12 | |
| 13 | setGifUrl(data[0]?.images?.downsized_medium.url); |
| 14 | } catch (error) { |
| 15 | setGifUrl("https://metro.co.uk/wp-content/uploads/2015/05/pokemon_crying.gif?quality=90&strip=all&zoom=1&resize=500%2C284"); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | useEffect(() => { |
| 20 | if (keyword) fetchGifs(); |
| 21 | }, [keyword]); |
| 22 | |
| 23 | return gifUrl; |
| 24 | }; |
| 25 | |
| 26 | export default useFetch; |
no test coverage detected