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

131
Views
How do I render a list from Json data in express js, then render it using react and map()?

I had a question, and a problem when rendering Json data with react js. I Couldn't get react to map out the data. Here is my code:

My Json:

res.json({
    list: [{id: 1, name: 'Caleb'}, {id: 2, name: 'Isaiah'}]
})

My React Code:

import { useState, useEffect } from 'react'
  
function App() {

  const [data, setData] = useState({})

  useEffect(() => {
    fetch("/home")
    .then(res => res.json())
    .then(data => setData(data))
  }, [])

  return (
    <div>
        {data.list.map(function(d, idx){
         return (<li key={idx}>{d.name}</li>)
       })}
    </div>
  );
}
  
export default App;

Its giving me this error, that it cant render it properly.

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

0

The simplest way to achieve your goal is to use conditional rendering (&&), you can put the JSON data in a variable, but it's not necessary. Another improvement is to use the object id as the key and not the index.

Here's an example of how to improve your code:

import { useState, useEffect } from 'react';

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

  useEffect(() => {
    fetch("/home")
    .then(res => res.json())
    .then(data => setData(data))
  }, [])

  return (
    <div>
      {data.list && (
        data.list.map(function (d) {
          return <li key={d.id}>{d.name}</li>;
        })
      )
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza Report

0

I fixed it by first putting the Json data into a variable, then made sure that the variable wasn't undefined, then rendered it! Like this:

import { useState, useEffect } from 'react'
  
function App() {

  const [data, setData] = useState({})

  useEffect(() => {
    fetch("/home")
    .then(res => res.json())
    .then(data => setData(data))
  }, [])

  const new_data = data.list;

  const renderAuth = () => {
    if (new_data !== undefined) {
      return (
        <div>
          {new_data.map(function(d, idx){
            return (<li key={idx}>{d.name}</li>)
          })}
        </div>
      );
    } else {
      console.log("sorry had problems")
    }
  }

  return (
    <div>
      {renderAuth()}
    </div>
  );

}
  
export default App;

Then it worked like a charm! No problems at all.

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!