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

232
Vistas
TypeError: tableData.map is not a function
import { useState, useEffect } from "react";

export default function RenderTable(){
    const [tableData, updateTableData] = useState([{id: 1, name: "name", price: 200, size: "size"}]);

    useEffect(()=>{
        fetch("/api/ManageSite").then(value => updateTableData(value.json()));
    }, []);

    return(
        <table>
            <tbody>
                {tableData.map(value =>
                    <tr key={value.id}>
                        <td>{value.name}</td>
                        <td>{value.price}</td>
                        <td>{value.size}</td>
                    </tr>
                )}
            </tbody>
        </table>
    )
}

this error occurs, when I use map function in return(), but if I will do the same map function in useEffect(), it will work just fine, please explain why?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This could happen because is trying to map an object and not an array, so it is saying that doesn't exists the function Object.map().

First, ensure that the new state is an array.

Then, if can get results different from an aray, when rendering, to not have any page crash, ensure that is making a map on an array

{Array.isArray(tableData) && tableData.map(()=>{})
about 4 years ago · Juan Pablo Isaza Denunciar

0

.json() returns a promise, which means in ES6 you need to use another then. Make sure the response from backend is array in JSON format.
Then you can just:

useEffect(()=>{
        fetch("/api/ManageSite")
          .then(value => value.json())
          .then(data => updateTableData(data))
    }, []);

Because no matter what even if you do fetch and update data like I did correctly but your endpoint response is not an array. Same error will keep occurring

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