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 =
<div className={"chartContent"}>
.
.
.
<div className="panel">
{DataRows.forEach(function(entry) &&
<div className="col-sm-2 graphPanel graphPanelExtra">
<div className="panel panel-primary">
<div>entry</div>
</div>
</div>
)}
</div>
</div>;
Use a map so you're actually returning the elements. A foreach doesn't return anything. Your use of &&
was also invalid.
var graph =
<div className="chartContent">
<div className="panel">
{DataRows.map(entry =>
<div key={entry.id} className="col-sm-2 graphPanel graphPanelExtra">
<div className="panel panel-primary">
<div style={{ textAlign: 'center' }}>entry</div>
</div>
</div>
)}
</div>
</div>;
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With