@@ -74,7 +74,7 @@ class App extends Component {
7474 this . setState ( ( state ) => ( {
7575 data : [ ...state . data , newData ] ,
7676 filteredData : [ ...state . data , newData ] ,
77- isPlayingIndex : state . data . length - 1 ,
77+ isPlayingIndex : state . data . length ,
7878 } ) ) ;
7979 } ) ;
8080 } ) ;
@@ -84,13 +84,12 @@ class App extends Component {
8484
8585 // functionality to change 'play' button to 'stop'
8686 setIsPlaying ( ) {
87- if ( this . state . isPlayingIndex > this . state . data . length - 1 ) {
88- this . setState ( { isPlayingIndex : 0 } ) ;
87+ if ( this . state . isPlayingIndex >= this . state . data . length ) {
88+ this . setState ( prevState => ( { ... prevState , isPlayingIndex : 0 } ) ) ;
8989 }
9090
9191 let { isPlaying } = this . state ;
92- isPlaying = ! isPlaying ;
93- this . setState ( { isPlaying } ) ;
92+ this . setState ( prevState => ( { ...prevState , isPlaying : ! prevState . isPlaying } ) ) ;
9493
9594 if ( isPlaying ) {
9695 this . actionInPlay ( ) ;
@@ -123,7 +122,7 @@ class App extends Component {
123122
124123 if ( isPlayingIndex >= data . length - 1 ) isPlayingIndex = 0 ;
125124
126- this . setState ( { isPlayingIndex : isPlayingIndex + 1 } ) ;
125+ this . setState ( prevState => ( { ... prevState , isPlayingIndex : isPlayingIndex + 1 } ) ) ;
127126 const { id, action, state } = data [ isPlayingIndex + 1 ] ;
128127 this . toTheFuture ( ) ;
129128
@@ -134,7 +133,7 @@ class App extends Component {
134133 if ( isPlaying && isPlayingIndex + 1 < data . length - 1 ) {
135134 this . actionInPlay ( ) ;
136135 } else {
137- this . setState ( { isPlaying : false } ) ;
136+ this . setState ( prevState => ( { ... prevState , isPlaying : false } ) ) ;
138137 }
139138 } , 1000 ) ;
140139 }
@@ -199,7 +198,7 @@ class App extends Component {
199198 direction : 'forward' ,
200199 } ) ;
201200
202- // if (isPlayingIndex >= this.state.data.length - 1) isPlayingIndex = 0 ;
201+ if ( this . state . isPlayingIndex >= this . state . data . length - 1 ) this . setState ( prev => ( { ... prev , isPlayingIndex : 0 } ) ) ;
203202
204203 const { data, isPlayingIndex } = this . state ;
205204 const { id, action, state } = data [ isPlayingIndex + 1 ] ;
0 commit comments