I'm new to React and am playing around with their Tic-Tac-Toe tutorial. I wanted to try generating the Squares using a for-loop but am running into an issue that I'm not quite understanding. That part of my code (placed at the bottom of the Board function) looks like this:
const retVal = Array(3).fill(null);
for (var i = 0; i < 10; i += 3) {
if (i < 3) {
retVal[0] = (
<>
<Square value={squares[i]} onSquareClick={() => handleClick(i)} />
<Square value={squares[i + 1]} onSquareClick={() => handleClick(i + 1)} />
<Square value={squares[i + 2]} onSquareClick={() => handleClick(i + 2)} />
</>
);
}
else if (i >= 3 && i < 6) {
retVal[1] = (
<>
<Square value={squares[i]} onSquareClick={() => handleClick(i)} />
<Square value={squares[i + 1]} onSquareClick={() => handleClick(i + 1)} />
<Square value={squares[i + 2]} onSquareClick={() => handleClick(i + 2)} />
</>
);
}
else {
retVal[2] = (
<>
<Square value={squares[i]} onSquareClick={() => handleClick(i)} />
<Square value={squares[i + 1]} onSquareClick={() => handleClick(i + 1)} />
<Square value={squares[i + 2]} onSquareClick={() => handleClick(i + 2)} />
</>
);
}
}
return (
<>
<div className="status">{status}</div>
<div className="board-row">
{retVal[0]}
</div>
<div className="board-row">
{retVal[1]}
</div>
<div className="board-row">
{retVal[2]}
</div>
</>
);
I realize this may not be the most elegant solution for generating the squares (and the tutorial mentions using two loops for this without showing an example) but that's a secondary question. The issue is that when handleClick(i)
gets called the value of i
received by this event handler is not what I was expecting. For example, when clicking the first square the value received for i
is 12 and not 0. Subsequently, all the other squares also have unexpected i
values. I've verified that i
is 0 when retVal[0]
is created so this appears to have something to do with how React renders the output since i
is being set to a value outside the range of the for-loop. Trying to understand why (or figure out if I'm missing an obvious bug.)