@@ -23,6 +23,19 @@ type CounterAction =
23
23
| { type : 'RESET' }
24
24
| { type : 'ADD' ; payload : number } ;
25
25
26
+ // New secondary reducer state and action types
27
+ type SecondaryCounterState = {
28
+ count : number ;
29
+ multiplier : number ;
30
+ lastOperation : string ;
31
+ } ;
32
+
33
+ type SecondaryCounterAction =
34
+ | { type : 'MULTIPLY' }
35
+ | { type : 'DIVIDE' }
36
+ | { type : 'SET_MULTIPLIER' ; payload : number }
37
+ | { type : 'RESET' } ;
38
+
26
39
function counterReducer ( state : CounterState , action : CounterAction , step : number ) : CounterState {
27
40
switch ( action . type ) {
28
41
case 'INCREMENT' :
@@ -64,6 +77,41 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
64
77
}
65
78
}
66
79
80
+ // New secondary reducer function
81
+ function secondaryCounterReducer (
82
+ state : SecondaryCounterState ,
83
+ action : SecondaryCounterAction ,
84
+ ) : SecondaryCounterState {
85
+ switch ( action . type ) {
86
+ case 'MULTIPLY' :
87
+ return {
88
+ ...state ,
89
+ count : state . count * state . multiplier ,
90
+ lastOperation : `Multiplied by ${ state . multiplier } ` ,
91
+ } ;
92
+ case 'DIVIDE' :
93
+ return {
94
+ ...state ,
95
+ count : state . count / state . multiplier ,
96
+ lastOperation : `Divided by ${ state . multiplier } ` ,
97
+ } ;
98
+ case 'SET_MULTIPLIER' :
99
+ return {
100
+ ...state ,
101
+ multiplier : action . payload ,
102
+ lastOperation : `Set multiplier to ${ action . payload } ` ,
103
+ } ;
104
+ case 'RESET' :
105
+ return {
106
+ count : 0 ,
107
+ multiplier : 2 ,
108
+ lastOperation : 'Reset' ,
109
+ } ;
110
+ default :
111
+ return state ;
112
+ }
113
+ }
114
+
67
115
function FunctionalReducerCounter ( {
68
116
initialCount = 0 ,
69
117
step = 1 ,
@@ -76,6 +124,8 @@ function FunctionalReducerCounter({
76
124
const [ clickCount , setClickCount ] = useState ( 0 ) ;
77
125
const [ lastClickTime , setLastClickTime ] = useState < Date | null > ( null ) ;
78
126
const [ averageTimeBetweenClicks , setAverageTimeBetweenClicks ] = useState < number > ( 0 ) ;
127
+
128
+ // First reducer
79
129
const [ state , dispatch ] = useReducer (
80
130
( state : CounterState , action : CounterAction ) => counterReducer ( state , action , step ) ,
81
131
{
@@ -85,6 +135,13 @@ function FunctionalReducerCounter({
85
135
} ,
86
136
) ;
87
137
138
+ // Second reducer
139
+ const [ secondaryState , secondaryDispatch ] = useReducer ( secondaryCounterReducer , {
140
+ count : initialCount ,
141
+ multiplier : 2 ,
142
+ lastOperation : 'none' ,
143
+ } ) ;
144
+
88
145
return (
89
146
< div
90
147
className = 'reducer-counter'
@@ -94,8 +151,10 @@ function FunctionalReducerCounter({
94
151
} }
95
152
>
96
153
< h2 > { title } </ h2 >
154
+
155
+ { /* Primary Counter Section */ }
97
156
< div className = 'counter-value' >
98
- < h3 > Current Count : { state . count } </ h3 >
157
+ < h3 > Primary Counter : { state . count } </ h3 >
99
158
</ div >
100
159
101
160
< div className = 'counter-buttons' >
@@ -118,7 +177,36 @@ function FunctionalReducerCounter({
118
177
) ) }
119
178
</ div >
120
179
</ div >
180
+
181
+ { /* Secondary Counter Section */ }
182
+ < div
183
+ className = 'secondary-counter'
184
+ style = { { marginTop : '2rem' , borderTop : '1px solid #ccc' , paddingTop : '1rem' } }
185
+ >
186
+ < h3 > Secondary Counter: { secondaryState . count } </ h3 >
187
+ < div className = 'counter-buttons' >
188
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'MULTIPLY' } ) } >
189
+ Multiply by { secondaryState . multiplier }
190
+ </ button >
191
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'DIVIDE' } ) } >
192
+ Divide by { secondaryState . multiplier }
193
+ </ button >
194
+ < button
195
+ onClick = { ( ) =>
196
+ secondaryDispatch ( { type : 'SET_MULTIPLIER' , payload : secondaryState . multiplier + 1 } )
197
+ }
198
+ >
199
+ Increase Multiplier
200
+ </ button >
201
+ < button onClick = { ( ) => secondaryDispatch ( { type : 'RESET' } ) } > Reset</ button >
202
+ </ div >
203
+ < div className = 'counter-info' >
204
+ < h4 > Last Operation: { secondaryState . lastOperation } </ h4 >
205
+ < h4 > Current Multiplier: { secondaryState . multiplier } </ h4 >
206
+ </ div >
207
+ </ div >
121
208
</ div >
122
209
) ;
123
210
}
211
+
124
212
export default FunctionalReducerCounter ;
0 commit comments