Created
March 9, 2022 15:19
-
-
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)
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 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