/useFetch.jsx Secret
Created
August 25, 2023 14:16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment