Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
How to display response data array in my React app?

I am trying to create a simple CRUD MERN Application. In this application data is displayed in my console, but I want it in my UI part.

Here is the console log: enter image description here

This is Alluser.js file where I want to display console data which is coming from my backend:

import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { getUsers } from "./api";

const UserData = () => {
  const [users, setUser] = useState([]);

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

  const AllUsers = async () => {
    const response = await getUsers();
    console.log(response.data);
    setUser(response.data);
  };

  return (
    <div>
      <div className="container">
        <table className="table table-hover table-bordered mt-3">
          <thead>
            <tr>
              <th scope="col">No</th>
              <th scope="col">Name</th>
              <th scope="col">Email</th>
              <th scope="col">Phone</th>
              <th scope="col">Action</th>
            </tr>
          </thead>
          <tbody>
            {users.map((index,user) => {
              <tr key={index}>
                <th scope="row">{user.id}</th>
                <td>{user.email}</td>
                <td>{user.name}</td>
                <td>{user.phone}</td>
   
              </tr>;
            })}

           
          </tbody>
        </table>
      </div>
    </div>
  );
};

export default UserData;
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It is not displaying, because of your map part, where you are not returning anything. Try this:

  {users.map((user, index) => (
      <tr key={index}>
        <th scope="row">{user.id}</th>
        <td>{user.email}</td>
        <td>{user.name}</td>
        <td>{user.phone}</td>
      </tr>;
  ))}
about 4 years ago · Juan Pablo Isaza Denunciar

0

{users.map((user,index) => {
              <tr key={index}>
                <th scope="row">{user.id}</th>
                <td>{user.email}</td>
                <td>{user.name}</td>
                <td>{user.phone}</td>
   
              </tr>;
            })}

Map function has 2 parameters which the first is your element

about 4 years ago · Juan Pablo Isaza Denunciar

0

When you are mapping over an array of values, the order of the parameters of the map function should be maintained which is

map((user) => { ... })
map((user, index) => { ... })
map((user, index, array) => { ... })

In your code, you have mentioned the index parameter before the user parameter which will give you undefined when you try to access the user.[anything].

if you change the order of user and index, it should work.

also, some free advice on the code : using the index as the key is not one of the best approaches as mentioned in the official docs, you can read more about it here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda