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

129
Vistas
map an array in componentDidMount and render

i'm traying to map an array in componentDidMount and render it but it doesn't show anything can anyone help me please?

componentDidMount() {
  baseUrl
      .get("/api/v1/bases")
        .then((response) => {
          const data = response.data.data.data.data;
          console.log(data)
             const items = data.map((item) => {
               return (
                 <div>
                   <tr key={item.id}>
                     <td scope="row">{item.id}</td>
                     <td scope="row">{item.name}</td>
                     <td scope="row">
                       <button className="btn btn btn-outline-danger m-1 my-2 my-sm-0">
                         حذف
                       </button>
                       <Link
                         id={item.id}
                         to={`/Bases/edit/${item.id}`}
                         className="btn btn btn-outline-warning m-1 my-2 my-sm-0"
                       >
                         ویرایش
                       </Link>
                     </td>
                   </tr>
                 </div>
               );
             });
          })
      .catch((err) => {
        console.log(err);
      });
}

and i am trying to render it like this code inside table

</tr>
          {this.items}
        </tbody>

it is just part of code

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

0

componentDidMount / useEffect are used to hook into the component's lifecycle. They are not meant to render/return any JSX elements. You could transform/map your data within componentDidMount and update the local component state which can then be used in render to display your data

class Component {
  state = [];

  async componentDidMount() {
    const items = await fetch()

    const mappedItems = items.map(item => item)

    this.setState(items)
  }

  render() {
    <ul>{this.state.map(item => (
      <li key={item.id}>
        {item}
      </li>
    ))}
    </ul>

  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just call your API in ComponenDidMount lifecycle and display your data into render function

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