Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda