@@ -105,12 +105,13 @@ class Game extends React.Component {
105105 history : [ {
106106 squares : Array ( 9 ) . fill ( null )
107107 } ] ,
108- xIsNext : true
108+ xIsNext : true ,
109+ stepNumber : 0
109110 } ;
110111 }
111112
112113 handleClick ( i ) {
113- const history = this . state . history ;
114+ const history = this . state . history . slice ( 0 , this . state . stepNumber + 1 ) ;
114115 const current = history [ history . length - 1 ] ;
115116 const squares = current . squares . slice ( ) ;
116117
@@ -123,15 +124,32 @@ class Game extends React.Component {
123124 history : history . concat ( [ {
124125 squares : squares ,
125126 } ] ) ,
127+ stepNumber : history . length ,
126128 xIsNext : ! this . state . xIsNext ,
127129 } ) ;
128130 }
129131
132+ jumpTo ( step ) {
133+ this . setState ( {
134+ stepNumber : step ,
135+ xIsNext : ( step % 2 ) === 0 ,
136+ } )
137+ }
138+
130139 render ( ) {
131140 const history = this . state . history ;
132- const current = history [ history . length - 1 ] ;
141+ const current = history [ this . state . stepNumber ] ;
133142 const winner = calculateWinner ( current . squares ) ;
134143
144+ const moves = history . map ( ( step , move ) => {
145+ const desc = move ? 'Go to move #' + move : 'Go to game start' ;
146+ return (
147+ < li key = { move } >
148+ < button onClick = { ( ) => this . jumpTo ( move ) } > { desc } </ button >
149+ </ li >
150+ )
151+ } )
152+
135153 let status ;
136154 if ( winner ) {
137155 status = 'Winner: ' + winner ;
@@ -148,8 +166,8 @@ class Game extends React.Component {
148166 />
149167 </ div >
150168 < div className = "game-info" >
151- < div > { /* status */ } </ div >
152- < ol > { /* TODO */ } </ ol >
169+ < div > { status } </ div >
170+ < ol > { moves } </ ol >
153171 </ div >
154172 </ div >
155173 ) ;
0 commit comments