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

153
Vistas
Unable to dinamically create elements in a loop using React Native

I´m trying to dinamically create some elements based on a list of JSON objects like this:

{Object.entries(this.state.topTen).forEach(([name]) => {
    console.log(name); // this correctly print the list of names
    React.createElement(MyCustomComponent, {name: name});
})}

I also tried the following approach inside the loop:

<MyCustomComponentname={name} />

But I don´t see anything rendered in the screen, I know for sure that this.state.topTen has the correct information because the console.log inside the loop is displaying the correct information. And if I add a <MyCustomComponentname={name} /> outside of the loop, the component is rendered correctly.

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

0

The method .forEach() will not return anything. You will need to use the .map() method of the array and return the result of React.createElement(MyCustomComponent, {name: name});.

For example:

{Object.entries(this.state.topTen).map(([name]) => {
    console.log(name); // this correctly print the list of names
    return React.createElement(MyCustomComponent, {name: name});
})}

Using .map() will return an array with whatever items you have returned within the callback. This resulting array can be rendered with React because the callback is returning the result of React.createElement(...).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

about 4 years ago · Juan Pablo Isaza Denunciar

0

I completely agree with the answer suggested by Bradon also it's better to pass the key prop from the array to the child component which will increase the performance of the app while re-rendering and also can eliminate unexpected issues. https://reactjs.org/docs/lists-and-keys.html go through the link for more clarity.

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