Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

63
Views
React for loops

I'm learning JavaScript / React. I'm trying to create a simple for loop for the tic-tac-toe example on the react website but so far its been problematic. Oddly, theres several map examples but no for loop examples.

Anyway, for the following:

function Row(props){
  function renderSquare (i) {
    return <Square key={i} value={props.squares[i]} onClick={() => props.onClick(i)} />;
  }
  const columns = 3;
  let ss = [];
  for(var a = 0, i = props.counter * columns; a < 3; a++, i++){
    //Test Code
  }
  return (
    <div className="board-row">
      {ss}
    </div>
  );
}

When I replace "Test Code", this works

ss.push(renderSquare(i));

But this fails

ss.push(<Square key={i} value={props.squares[i]} onClick={() => props.onClick(i)}/>);

The second option fails since its not creating "separate" onClick elements. How can I make the failing example work?


Update #1

Sample

Update #2 I changed the var to let as recommended by @Matthias247 and it now works :D

function Row(props){
  function renderSquare (i) {
    return <Square key={i} value={props.squares[i]} onClick={() => props.onClick(i)} />;
  }
  const columns = 3;
  let ss = [];
  for(let a = 0, i = props.counter * columns; a < columns; a++, i++){
    ss.push(<Square key={i} value={props.squares[i]} onClick={() => props.onClick(i)}/>);
    //ss.push(renderSquare(i));
    //console.log(i);
  }
  return (
    <div className="board-row">
      {ss}
    </div>
  );
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think the problem is that you capture i into the element and the onClick closure. You change i during the for loop, but in the end it's the same for all inserted elements -> props.counter + 3.

To fix it you need to create a unique instance of all captured values for each element. This is exactly what you do with the renderSquare method which creates a new instance of i inside the function body, which is now captured by the element.

Edit:

Using a let binding, which creates a fresh instance of the variable per iteration, also works:

for (let a = 0, i = props.counter; a < 3; a++, i++) {
    ss.push(<Square key={i} value={props.squares[i]} onClick={() => props.onClick(i)}/>);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!