Friday, 16 February 2018

javascript - Use For loop inside JSX





Can you use a for loop in JSX like this?
Or rather, what is the propper way to write a for like this?



var graph = 



.
.
.


{DataRows.forEach(function(entry) &&


entry




)}


;

Answer



Use a map so you're actually returning the elements. A foreach doesn't return anything. Your use of && was also invalid.



var graph = 



{DataRows.map(entry =>


entry



)}


;


Note that I added a key property - for react to optimise rendering of an array of elements each item should have a unique key (that isn't just the numeric index).


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...