1611 shaares
17 private links
17 private links
1 result
tagged
loop
Think of it like you're just calling JavaScript functions. You can't put a for
loop inside a function call:
return tbody(
for (var i = 0; i < numrows; i++) {
ObjectRow()
}
)
But you can make an array, and then pass that in:
var rows = [];
for (var i = 0; i < numrows; i++) {
rows.push(ObjectRow());
}
return tbody(rows);
You can use basically the same structure when working with JSX:
var rows = [];
for (var i = 0; i < numrows; i++) {
// note: we add a key prop here to allow react to uniquely identify each
// element in this array. see: https://reactjs.org/docs/lists-and-keys.html
rows.push(<ObjectRow key={i} />);
}
return <tbody>{rows}</tbody>;
Incidentally, my JavaScript example is almost exactly what that example of JSX transforms into. Play around with Babel REPL to get a feel for how JSX works.