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

282
Views
Data is fetching properly but why is it not displaying?

I am using react hooks to fetch an api and show the list of NBA players first and last name. Fetching API is successful as it can be shown in log but it's not displaying the names.
Free API I used:https://www.balldontlie.io/

//react component to fetch and show data

import { useCallback, useEffect, useState } from "react";

function App() {
  const [data, setData] = useState([]);

  const refreshData = useCallback(() => {
    fetch("https://www.balldontlie.io/api/v1/players")
      .then((res) => res.json())
      .then((data) => {
        setData(data);
        console.log(data);
      });
  }, []);

  useEffect(() => {
    refreshData();
  }, []);

  return (
    <div>
      <h1>My player</h1>
      <ul>
        {Object.values(data).map((item) => (
          <li key={item.id}>
            {item.first_name} {item.last_name}
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Issues

The data is an object with two keys, data and meta, neither of which are objects of players with first and last name properties.

// 20211018232429
// https://www.balldontlie.io/api/v1/players

{
  "data": [
    {
      "id": 14,
      "first_name": "Ike",
      ...
      "last_name": "Anigbogu",
      ...
    },
    ...
    {
      "id": 497,
      "first_name": "Michael",
      ...
      "last_name": "Ansley",
      ...
    }
  ],
  "meta": {
    ....
  }
}

Solution

You want to map the data property which is the array of player objects.

function App() {
  const [data, setData] = useState([]);

  const refreshData = useCallback(() => {
    fetch("https://www.balldontlie.io/api/v1/players")
      .then((res) => res.json())
      .then((data) => {
        setData(data.data); // <-- save data.data array
      });
  }, []);

  useEffect(() => {
    refreshData();
  }, [refreshData]);

  return (
    <div>
      <h1>My player</h1>
      <ul>
        {data.map((item) => ( // <-- map data array
          <li key={item.id}>
            {item.first_name} {item.last_name}
          </li>
        ))}
      </ul>
    </div>
  );
}

function App() {
  const [data, setData] = React.useState([]);

  const refreshData = React.useCallback(() => {
    fetch("https://www.balldontlie.io/api/v1/players")
      .then((res) => res.json())
      .then((data) => {
        setData(data.data);
      });
  }, []);

  React.useEffect(() => {
    refreshData();
  }, [refreshData]);

  return (
    <div>
      <h1>My player</h1>
      <ul>
        {data.map((item) => (
          <li key={item.id}>
            {item.first_name} {item.last_name}
          </li>
        ))}
      </ul>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(
  <App />,
  rootElement
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root" />

about 4 years ago · Juan Pablo Isaza Report

0

You didn't update state with actual value. you passed object into state. Here is what you have to just pass to access the values:

const refreshData = useCallback(() => {
fetch("https://www.balldontlie.io/api/v1/players")
.then((res) => res.json())
.then((data) => {
setData(data.data);
console.log(data);
      });
  }, []);
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!