Friday, February 16, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...