Skip to content

Commit 9cc9d7f

Browse files
committed
verified multiple reducers render on front end component map
1 parent a141020 commit 9cc9d7f

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

demo-app/src/client/Components/FunctionalReducerCounter.tsx

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ type CounterAction =
2323
| { type: 'RESET' }
2424
| { type: 'ADD'; payload: number };
2525

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+
2639
function counterReducer(state: CounterState, action: CounterAction, step: number): CounterState {
2740
switch (action.type) {
2841
case 'INCREMENT':
@@ -64,6 +77,41 @@ function counterReducer(state: CounterState, action: CounterAction, step: number
6477
}
6578
}
6679

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+
67115
function FunctionalReducerCounter({
68116
initialCount = 0,
69117
step = 1,
@@ -76,6 +124,8 @@ function FunctionalReducerCounter({
76124
const [clickCount, setClickCount] = useState(0);
77125
const [lastClickTime, setLastClickTime] = useState<Date | null>(null);
78126
const [averageTimeBetweenClicks, setAverageTimeBetweenClicks] = useState<number>(0);
127+
128+
// First reducer
79129
const [state, dispatch] = useReducer(
80130
(state: CounterState, action: CounterAction) => counterReducer(state, action, step),
81131
{
@@ -85,6 +135,13 @@ function FunctionalReducerCounter({
85135
},
86136
);
87137

138+
// Second reducer
139+
const [secondaryState, secondaryDispatch] = useReducer(secondaryCounterReducer, {
140+
count: initialCount,
141+
multiplier: 2,
142+
lastOperation: 'none',
143+
});
144+
88145
return (
89146
<div
90147
className='reducer-counter'
@@ -94,8 +151,10 @@ function FunctionalReducerCounter({
94151
}}
95152
>
96153
<h2>{title}</h2>
154+
155+
{/* Primary Counter Section */}
97156
<div className='counter-value'>
98-
<h3>Current Count: {state.count}</h3>
157+
<h3>Primary Counter: {state.count}</h3>
99158
</div>
100159

101160
<div className='counter-buttons'>
@@ -118,7 +177,36 @@ function FunctionalReducerCounter({
118177
))}
119178
</div>
120179
</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>
121208
</div>
122209
);
123210
}
211+
124212
export default FunctionalReducerCounter;

demo-app/src/client/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
4-
<title>Reactime</title>
4+
<title>Demo App</title>
55
<!-- <link rel="icon" type="image/x-icon" href="../../../assets/logos/blackWhiteSquareIcon.png"> -->
6-
<link rel="stylesheet" type="text/css" href="./style.css">
6+
<link rel="stylesheet" type="text/css" href="./style.css" />
77
</head>
88
<body>
99
<main id="root"></main>

0 commit comments

Comments
 (0)