Last active
March 20, 2019 19:01
-
-
Save jaysoo/14ba263e93114b600f7c77077cba04de to your computer and use it in GitHub Desktop.
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
// libs/gifs/src/lib/gifs.tsx | |
export const Gifs = (props: GifsProps) => { | |
// ... | |
const getFetchUrl = useCallback( | |
() => | |
// Return `null` if query is empty | |
query | |
? `https://api.giphy.com/v1/gifs/search?api_key=${ | |
props.apiKey | |
}&q=${query}` | |
: null, | |
[query] | |
); | |
useEffect( | |
() => { | |
async function fetchData() { | |
const url = getFetchUrl(); | |
if (url) { | |
const result = await axios(url); | |
// ... | |
} else { | |
// Skip API request if url not found | |
setGifs([]); | |
} | |
} | |
fetchData(); | |
}, | |
[getFetchUrl] | |
); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment