File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -51,12 +51,13 @@ import './index.css';
5151 history :[ {
5252 squares : Array ( 9 ) . fill ( null )
5353 } ] ,
54+ stepNumber : 0 ,
5455 xIsNext : true
5556 }
5657 }
5758
5859 handleClick ( i ) {
59- const history = this . state . history ;
60+ const history = this . state . history . slice ( 0 , this . state . stepNumber + 1 ) ;
6061 const current = history [ history . length - 1 ] ;
6162 const squares = current . squares . slice ( ) ;
6263 if ( calculateWinner ( squares ) || squares [ i ] ) {
@@ -67,15 +68,34 @@ import './index.css';
6768 history : history . concat ( [ {
6869 squares : squares
6970 } ] ) ,
71+ stepNumber : history . length ,
7072 xIsNext : ! this . state . xIsNext
7173 } ) ;
7274 }
7375
76+ jumpTo ( step ) {
77+ this . setState ( {
78+ stepNumber : step ,
79+ xIsNext : ( step % 0 ) === 0
80+ } )
81+ }
82+
7483 render ( ) {
7584 const history = this . state . history ;
76- const current = history [ history . length - 1 ] ;
85+ const current = history [ this . state . stepNumber ] ;
7786 const winner = calculateWinner ( current . squares ) ;
7887
88+ const moves = history . map ( ( step , move ) => {
89+ const desc = move ?
90+ 'Go to move #' + move :
91+ 'Go to game start' ;
92+ return (
93+ < li key = { move } >
94+ < button onClick = { ( ) => this . jumpTo ( move ) } > { desc } </ button >
95+ </ li >
96+ )
97+ } ) ;
98+
7999 let status ;
80100 if ( winner ) {
81101 status = 'Winner: ' + winner ;
@@ -93,7 +113,7 @@ import './index.css';
93113 </ div >
94114 < div className = "game-info" >
95115 < div > { status } </ div >
96- < ol > { /* TODO */ } </ ol >
116+ < ol > { moves } </ ol >
97117 </ div >
98118 </ div >
99119 ) ;
You can’t perform that action at this time.
0 commit comments