Think of it like you're just calling JavaScript functions. You can't use a for loop where the arguments to a function call would go:
return tbody(
for (let i = 0; i < numrows; i++) {
ObjectRow()
}
)
See how the function tbody is being passed a for loop as an argument – leading to a syntax error.
But you can make an array, and then pass that in as an argument:
const rows = [];
for (let i = 0; i < numrows; i++) {
rows.push(ObjectRow());
}
return tbody(rows);
You can basically use the same structure when working with JSX:
const rows = [];
for (let i = 0; i < numrows; i++) {
// note: we are adding 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.
Answer from Sophie Alpert on Stack OverflowThink of it like you're just calling JavaScript functions. You can't use a for loop where the arguments to a function call would go:
return tbody(
for (let i = 0; i < numrows; i++) {
ObjectRow()
}
)
See how the function tbody is being passed a for loop as an argument – leading to a syntax error.
But you can make an array, and then pass that in as an argument:
const rows = [];
for (let i = 0; i < numrows; i++) {
rows.push(ObjectRow());
}
return tbody(rows);
You can basically use the same structure when working with JSX:
const rows = [];
for (let i = 0; i < numrows; i++) {
// note: we are adding 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.
I am not sure if this will work for your situation, but often map is a good answer.
If this was your code with the for loop:
<tbody>
for (var i=0; i < objects.length; i++) {
<ObjectRow obj={objects[i]} key={i}>
}
</tbody>
You could write it like this with map:
<tbody>
{objects.map(function(object, i){
return <ObjectRow obj={object} key={i} />;
})}
</tbody>
ES6 syntax:
<tbody>
{objects.map((object, i) => <ObjectRow obj={object} key={i} />)}
</tbody>
let sampleOutput =()=>{
return
for(let i=0;i<3;i++){<p>hi</p>}
}
trying to display hi 3 times inside
return()in my component. I know how to .map or .forEach for objects and arrays but what if my store or variable is just a number, I assume forloop doesnt work?
The render method of your component, or your stateless component function, returns the elements to be rendered.
If you want to use a loop, that's fine:
render() {
let menuItems = [];
for (var i = 0; i < Users.length; i++) {
menuItems.push(<MenuItem eventKey=[i]>User.firstname[i]</MenuItem>);
}
return <div>{menuItems}</div>;
}
More common would be to see a more functional style, such as using a map to return the array of elements:
render() {
return <div>
{
Users.map((user, i) =>
<MenuItem eventKey=[i]>User.firstname[i]</MenuItem>)
}
</div>;
}
Note that in either case, you are missing the key property from each element of your array, so you will see warnings. Each element in an array should have a unique key, preferably some form of ID rather than just the array index.
With map you can do:
Users.map((user, index) => (
<MenuItem eventKey={index}>user.firstname</MenuItem>
));