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

171
Views
Rendering a list of HTML elements with data returned by server

I have an array of objects returned by the server which I have to display in a table. I tried using array.map() method to map the the objects as row elements into another array and then displaying that array in the JSX like <tbody>{listItems}</tbody>

const TeacherTable = () => {
  let listItems
  async function getTeacherData() {
    const response = await fetch('http://localhost:1234/api/teacher')
    const res = await response.json()
    console.log(res.data)
    listItems = await res.data.map(record => (
      <tr>
        <td>{record.teacherID}</td>
        <td>{record.teacherName}</td>
        <td>{record.teacherEmail}</td>
      </tr>
    ))
  }
  useEffect(() => {
    getTeacherData()
  })
  return <tbody>{listItems}</tbody>;
};

I don't know what I'm doing wrong but I doesn't seem to work. I tried to console.log() the data array to check if the data is getting passed to the frontend and its getting through without any errors but still the list doesn't get rendered.

How do I render this list of row elements from the array?

Thank you for reading this far. Hope you have a great day/night ahead.

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

0

There are a few things you should do:

  1. listItems needs to be a state variable meaning that it causes a re-render of the component when it gets updated, because right now when listItems is set inside of getTeacherData the component isn't re-rendering. I've implemented it as state in the updated code below (called items now). If you want to read more about state and how it causes updates you should check this out
  2. You should pass an empty array as dependencies to useEffect. The short explaination as to why is because you only want useEffect to be called once, for a more thorough explaination you should read this.
  3. Kinda building off (1), it's arguably a better practice to have state only contain data and not the component logic. In this example you'll see that I didn't map out the fetched data as jsx, rather I just stored it's contents and mapped it out inside the return part of the component.

import {useState, useEffect} from "react";

const TeacherTable = () => {
  const [items, setItems] = useState([]);
  
   useEffect(() => {
      async function getTeacherData() {
            const response = await fetch('http://localhost:1234/api/teacher')
            const res = await response.json()
            console.log(res.data)
            setItems(res.data);
        }

        getTeacherData();
    }, []);
  
  return <tbody>{items.map(record => (
      <tr>
        <td>{record.teacherID}</td>
        <td>{record.teacherName}</td>
        <td>{record.teacherEmail}</td>
      </tr>
    ))}</tbody>;
};

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!