0

i tried to loop and execute a component inside react, but giving error. Map function is used when we have an array, but here i have a number and i want to execute a component 4 number of times.

Error it was giving is : "Parsing error: Unexpected token (61:23)"

<div>
(
for (var i=0; i <4; i++) {
       <StarIcon />
 }
)
</div>

2 Answers2

0

There is a better way to display your stars :)

  <div>
    {Array.from({ length: 5 }).map((_, i) => (
      <StarIcon key={i} />
    ))}
  </div>
0

In your form of approach this is how you can properly perform displaying the component you want in a loop.

const REPEATED_COUNTS = 4;

const components = [];
  for (let i = 0; i <= REPEATED_COUNTS; i++) {
    components.push(<Stars key={i} />);
 }
  return (
    ...
    <div>{components}</div>
  );
};