Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!