Bitcoin Price Fetcher

Create a custom hook to regularly fetch the price of Bitcoin.


Use the environment you are most comfortable with. I recommend using create-react-app to create a local version of the project so that you can inspect the DOM easily from your browser when debugging. Alternatively, you can work from a blank React template in CodeSandbox. Starter code is provided after the problem specification.


Write a custom hook called useBitcoin() that returns the value of Bitcoin in US dollars. Every minute, the price should be re-fetched and the returned variable should be updated (this is the same rate at which the API updates the price). To retrieve the price of Bitcoin in US dollars, issue a GET request to the following URL: The free API returns a JSON of the following form:

    "updated":"Dec 28, 2023 23:35:00 UTC",
    "updateduk":"Dec 28, 2023 at 23:35 GMT"
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from",
      "description":"United States Dollar",
      "description":"British Pound Sterling",

In the solution below, I use the Axios library to issue the GET request, but you should use the data-fetching library that you are most comfortable with. While developing, be careful not to accidentally issue requests to the API in an infinite loop so as not to have later requests blocked. If you are unfamiliar with building custom hooks in React, you can read the documentation here. Finally, render a component that makes use of your custom hook and displays the price on the screen. While the value returned from the hook is undefined, display a loading screen instead.

Starter Code

Here is some code to get you started:

import React, { useEffect, useState } from "react";
import axios from "axios";

const useBitcoin = () => {

function App() {

export default App;

For comparison as you develop, the solution below is roughly 35 lines.


import React, { useEffect, useState } from "react";
import axios from "axios";

const useBitcoin = () => {
  const [value, setValue] = useState();

  useEffect(() => {
    const fetchValue = async () => {
      try {
        const res = await axios.get(
      } catch {
        console.error("Failed to update price");


    const interval = setInterval(() => fetchValue(), 60000);
    return () => clearInterval(interval);
  }, []);

  return value;

function App() {
  const value = useBitcoin();

  return (
    <div style={{ display: "flex", justifyContent: "center" }}>
      {value ? (
        <h1>{"Bitcoin Price: $" + value + " USD"}</h1>
      ) : (
        <h4>Fetching price...</h4>

export default App;

The useBitcoin() custom hook wraps two built-in React hooks, useState() and useEffect(), and returns the variable initialized with useState(). The callback function passed to useEffect() defines an asynchronous function to fetch the current bitcoin price and set the state variable to that price. That asynchronous function is then called (so that the price is fetched when the component first mounts) and sets up an interval to recall the async function every minute. We also include an empty dependency array as the second argument to the useEffect() function; without it, the function passed via useEffect()’s first argument would render on every render. Because the function we use updates state, this would create an infinite loop. Finally, we use a ternary operator to conditionally render the loading screen or the fetched price.