I was following some tutorials on react web-site and tried some example code included. Here's the link for that code in codepen
https://codepen.io/gaearon/pen/gWWZgR?editors=0010
Here are some snippets to the problem
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
This will call the renderSquare method passing a number as an argument so this will be helpful for identifying onClick method depending on the square (Which is a button).
Here's the renderSquare method
renderSquare(i) {
console.log(<Square/>);
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
and this method calls a functional component Square,
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
So when a square is clicked it will calls to handleClick(i) method (Better to see codepen link so you'll understand the whole code)
handleClick(i) {
console.log(i);
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([{
squares: squares,
}]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext,
});
}
So by using that console.log (Not in codepen code), when I click a button (tic-tac-toe game, so 9 buttons) it will shows the argument which is passed (0,1,2...8).
So my question is where these numbers are stored in those rendered react components? I tried console logging Square component but I couldn't find that argument. (This is not related to props or state)