Skip to content

Instantly share code, notes, and snippets.

@ibelick
Created March 9, 2022 15:19
Show Gist options
  • Save ibelick/90e15dec5e7b71b41e5fdd88ef3e981f to your computer and use it in GitHub Desktop.
Save ibelick/90e15dec5e7b71b41e5fdd88ef3e981f to your computer and use it in GitHub Desktop.
Hook to get the current price of any cryptocurrencies in any other supported currencies that you need. (Coingecko API)
import useSWR from "swr";
import { fetcher } from "lib/fetch";
const usePrice = (fromCurrencies: string, vsCurrencies: string) => {
const { data, error } = useSWR(
`https://api.coingecko.com/api/v3/simple/price?ids=${fromCurrencies}&vs_currencies=${vsCurrencies}`,
fetcher
);
return {
data,
isLoading: !error && !data,
isError: !data?.id,
};
};
// ex: `const { data } = usePrice("ethereum", "usd");`
export default usePrice;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment