Skip to content

Commit

Permalink
use redux store to hold step number
Browse files Browse the repository at this point in the history
  • Loading branch information
riku929hr committed Jan 2, 2022
1 parent e5e42d8 commit 9c071f9
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Game.js
Expand Up @@ -14,28 +14,26 @@ class Game extends React.Component {
squares: Array(9).fill(null),
},
],
stepNumber: 0,
};
}

handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const history = this.state.history.slice(0, this.props.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.props.xIsNext ? "X" : "O";

this.props.place(this.state.stepNumber, this.state.xIsNext);
this.props.place(this.props.stepNumber, this.props.xIsNext);

this.setState({
history: history.concat([
{
squares: squares,
},
]),
stepNumber: history.length,
});
}

Expand All @@ -48,7 +46,7 @@ class Game extends React.Component {

render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const current = history[this.props.stepNumber];
const winner = calculateWinner(current.squares);

return (
Expand Down

0 comments on commit 9c071f9

Please sign in to comment.