From 9c071f968e52f1f6e4cbc1be797ca46b8d5a7906 Mon Sep 17 00:00:00 2001 From: riku929hr Date: Sun, 2 Jan 2022 15:45:27 +0900 Subject: [PATCH] use redux store to hold step number --- src/Game.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Game.js b/src/Game.js index 9a7df16..526ed4d 100644 --- a/src/Game.js +++ b/src/Game.js @@ -14,12 +14,11 @@ 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]) { @@ -27,7 +26,7 @@ class Game extends React.Component { } 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([ @@ -35,7 +34,6 @@ class Game extends React.Component { squares: squares, }, ]), - stepNumber: history.length, }); } @@ -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 (