Skip to content

Instantly share code, notes, and snippets.

@jaysoo
Last active March 20, 2019 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaysoo/14ba263e93114b600f7c77077cba04de to your computer and use it in GitHub Desktop.
Save jaysoo/14ba263e93114b600f7c77077cba04de to your computer and use it in GitHub Desktop.
// 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