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

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

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 Report

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 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!