I am creating a 3 x 3 table through a simple loop as following:
React.useEffect(() => {
const _array = [];
let i = -1;
for (let c = 1; c < 4; c++) {
for (let r = 1; r < 4; r++) {
i++;
_array.push(
<div
key={`x${c}x${r}`}
className={`x${c}x${r}`}
onClick={() => console.log(i)}
>
{i}
</div>
);
}
}
setArray(_array);
}, []);
The console.log(i) in each <div> returns 8 although the {i} prints the correct number each time.
How can I code the correct i inside each div using this loop?
To reproduce you can see the full code on CodeSandbox.