Skip to content

Commit 58c47e1

Browse files
committed
5th step: Added history buttons functionality.
1 parent fb6953c commit 58c47e1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ class Game extends React.Component {
1414
history: [{
1515
squares: Array(9).fill(null), // Now Board Component will not store the state of the game any more.
1616
}],
17+
stepNumber: 0,
1718
xIsNext: true,
1819
}
1920
}
2021

2122
handleClick(i) { /* Due Game Component now have the squares state and Board Component only uses their props this Coponent will
2223
* handleClicks.*/
24+
this.history = this.state.history.slice(0, this.state.stepNumber + 1);
25+
this.current = this.history[this.history.length - 1];
2326
const squares = this.current.squares.slice();
2427
if (this.winner || squares[i]) {
2528
return;
@@ -29,15 +32,24 @@ class Game extends React.Component {
2932
history: this.history.concat([{
3033
squares: squares,
3134
}]),
35+
stepNumber: this.history.length,
3236
xIsNext: !this.state.xIsNext,
3337
});
3438
}
3539

40+
jumpTo(step) {
41+
this.setState({
42+
stepNumber: step,
43+
xIsNext: (step % 2) === 0,
44+
});
45+
}
46+
3647
render() {
3748
const MAX_LEN = 9;
3849
this.history = this.state.history;
39-
this.current = this.history[this.history.length-1];
40-
this.winner = calculateWinner(this.current.squares); // Due Game Component now have the squares state this Component will calculateWinner.
50+
this.current = this.history[this.state.stepNumber];
51+
this.winner = calculateWinner(this.current.squares); /* Due Game Component now have the squares state this Component will
52+
* calculateWinner.*/
4153

4254
const moves = this.history.map((step, move) => {
4355
const desc = move ? 'Go to move #' + move : 'Go to game start';

0 commit comments

Comments
 (0)