Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
How do I display data from an external API on react js?

Im building an app where I want to take api data from https://www.thecocktaildb.com to allow for users to search for a cocktail drink and it will fetch data from the api source to display the name of the drink on the page. I don't know why its giving me an error of "Uncaught TypeError: drinkList.drinks is undefined" because if you look at the screenshot I included of what the JSON data looks like, it should be correct?

This is my Home.js

import React, { useEffect, useState } from "react";
import axios from "axios";
import Drinks from "../components/Drinks";

function Home() {
  const [drinkName, setDrinkName] = useState();

 const drinksURL = `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${drinkName}`;

  function handleChangeDrink(e) {
    setDrinkName(e.target.value);
  }

  const getDrink = () => {
    axios
      .get(drinksURL)
      .then(function (response) {
        setDrinkName(response.data);
        console.log(drinksURL);
      })
      .catch(function (error) {
        console.warn(error);
      });
  };

  return (
    <main className="App">
      <section className="drinks-section">
        <input
          type="text"
          placeholder="Name of drink (e.g. margarita)"
          onChange={handleChangeDrink}
        />
        <button onClick={getDrink}>Get a Drink Recipe</button>
        <Drinks drinkList={drinkName} />
      </section>
    </main>
  );
}

export default Home;

and this is my Drinks.js component

import React from "react";

function Drinks({ drinkList }) {
  if (!drinkList) return <></>;
  return (
    <section className="drinkCard">
      <h1>{drinkList.drinks[0].strDrink}</h1>
    </section>
  );
}

export default Drinks;

This is a screenshot of the JSON data: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should define the new variable for drink list

const [drinkList, setDrinkList] = useState([]);

And you should assign your response to this variable here (instead of assigning drinkName):

 const getDrink = () => {
    axios
      .get(drinksURL)
      .then(function (response) {
        setDrinkList(response.data);
        console.log(drinksURL);
      })
      .catch(function (error) {
        console.warn(error);
      });
  };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!