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

111
Views
How can I read data updated to useState from a useEffect?

I've seen similar questions to this, but none of them answer my question. For the code, it's this.

  const [raceData, updateRaceData] = useState();
  const [raceChosen, updateRaceChosen] = useState();

  const url = "http://localhost:5000/races/viewraces";

  useEffect(() => {
    Axios.get(url)
      .then((response) => {
        const allRaces = response.data.races;
        updateRaceData(allRaces);
      })
      .catch((error) => console.error(`Error: ${error}`));
  }, []);


  console.log(raceData[0]);

So when I console.log() raceData and raceData[0] or raceData1, it works and doesn't give me any trouble. But if I instead do console.log(raceData[0].race), and then refresh, it starts telling me :

TypeError: Cannot read properties of undefined (reading 'race')

Any help on figuring out why trying to access things from the response I'm getting doesn't fully work would be appreciated.

Edit: Here is the object response with raceData[1]: Response object

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

0

The browser is running console.log(raceData[0]) before the variable being assigned. Try raceData && console.log(raceData[0]); which will only execute the console.log() if raceData is assigned.

about 4 years ago · Juan Pablo Isaza Report

0

Try mapping over the racedata as such:

raceData?.map((race) => console.log(race))

this should populate your raceData info

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!