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
handleClick(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,
});
console.log(this.state.history);
}
render() {
console.log(this.state.history);
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((val, index) => {
const desc = index ? 'Go to move #' + index : 'Go to game start';
return (
<li key={index}>
<button onClick={() => this.jumpTo(index)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
My question is
console.log
in handleClick method and
console.log
in render method shows 2 different answers. But they should show the same as I can understand because the console.log of handleClick is after set State. Is it because set State takes some time? or any other thing?