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

121
Visualizações
using forEch to render react components

I'm trying to use forEach to render my components.

{myArray.forEach((value, index) => {
    <MyComponent {...value} />
})}

this is not doing anything. but if I do

{myArray.forEach((value, index) => {
    console.log(index, value)
})}

//also tried with...

{myArray.forEach((index) => {
    <MyComponent {...myArray[index]} />;
})}

I see the element in the console. why is that? I read that forEach returns undefined and I should use map to iterate but why it shows the object in the console but doesn't render my component? thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Foreach doesn't return anything. But for rendering any component inside the jsx you have to return an array of JSX.Elements or HTMLElements. Try using map instead of forEach.

{array.map((value, index) => {
    return <MyComponent {...value} />
})}
about 4 years ago · Juan Pablo Isaza Relatório

0

first thing forEach function does not return a value but rather only executing the code that is inside of it, therefor you see the console.logs.

In React it is best practice to use the map function.

So instead of doing this

{array.forEach((value, index) => {
    <MyComponent {...value} />
})}

Do this

{array.map((value, index) => {
    return <MyComponent {...value} />
})}
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