1- import React , { useState } from 'react' ;
2-
3- function CounterFrame ( ) {
4- const numbers = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ] ;
5- const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ;
6- const [ counters , setCounters ] = useState ( [ 0 , 0 , 0 , 0 ] ) ;
7- const [ processedLast , setProcessedLast ] = useState ( false ) ;
1+ import React from 'react' ;
2+ import { create } from 'zustand'
83
4+ const useCounterStore = create ( ( set ) => ( {
5+ numbers : [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] ,
6+ index : 0 ,
7+ guesses : [ {
8+ correct : 0 ,
9+ incorrect : 0
10+ } , {
11+ correct : 0 ,
12+ incorrect : 0
13+ } , {
14+ correct : 0 ,
15+ incorrect : 0
16+ } , {
17+ correct : 0 ,
18+ incorrect : 0
19+ } ] ,
20+ increment : ( guess ) => set ( ( state ) => {
21+ if ( state . index === numbers . length - 1 ) {
22+ return state
23+ }
24+ if ( ! [ 1 , 2 , 3 , 4 ] . includes ( guess ) ) {
25+ return state
26+ }
27+ const guesses = [ ...state . guesses ] ;
28+ guesses [ guess - 1 ] += 1 ;
29+ return ( { index : state . index + 1 , guesses } )
30+ } ) ,
31+ } ) )
932
33+ function CounterFrame ( ) {
1034 const handleButtonClick = ( counterIndex ) => {
1135 //Checks to make sure we haven't iterated through the last element yet
1236 if ( ! processedLast ) {
@@ -42,4 +66,4 @@ function CounterFrame() {
4266 ) ;
4367}
4468
45- export default CounterFrame ;
69+ export default CounterFrame ;
0 commit comments