Skip to content

Commit c29ac21

Browse files
committed
added function based reducer counter in demo app
1 parent 9f61e44 commit c29ac21

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import React, { useReducer } from 'react';
2+
3+
type CounterState = {
4+
count: number;
5+
history: number[];
6+
lastAction: string;
7+
};
8+
9+
type CounterAction =
10+
| { type: 'INCREMENT' }
11+
| { type: 'DECREMENT' }
12+
| { type: 'DOUBLE' }
13+
| { type: 'RESET' }
14+
| { type: 'ADD'; payload: number };
15+
16+
const initialState: CounterState = {
17+
count: 0,
18+
history: [],
19+
lastAction: 'none',
20+
};
21+
22+
function counterReducer(state: CounterState, action: CounterAction): CounterState {
23+
switch (action.type) {
24+
case 'INCREMENT':
25+
return {
26+
...state,
27+
count: state.count + 1,
28+
history: [...state.history, state.count + 1],
29+
lastAction: 'INCREMENT',
30+
};
31+
case 'DECREMENT':
32+
return {
33+
...state,
34+
count: state.count - 1,
35+
history: [...state.history, state.count - 1],
36+
lastAction: 'DECREMENT',
37+
};
38+
case 'DOUBLE':
39+
return {
40+
...state,
41+
count: state.count * 2,
42+
history: [...state.history, state.count * 2],
43+
lastAction: 'DOUBLE',
44+
};
45+
case 'RESET':
46+
return {
47+
...initialState,
48+
lastAction: 'RESET',
49+
};
50+
case 'ADD':
51+
return {
52+
...state,
53+
count: state.count + action.payload,
54+
history: [...state.history, state.count + action.payload],
55+
lastAction: `ADD ${action.payload}`,
56+
};
57+
default:
58+
return state;
59+
}
60+
}
61+
62+
function FunctionalReducerCounter(): JSX.Element {
63+
const [state, dispatch] = useReducer(counterReducer, initialState);
64+
65+
return (
66+
<div className='reducer-counter'>
67+
<h2>Function-based Reducer Counter</h2>
68+
<div className='counter-value'>
69+
<h3>Current Count: {state.count}</h3>
70+
</div>
71+
72+
<div className='counter-buttons'>
73+
<button onClick={() => dispatch({ type: 'INCREMENT' })}>Increment (+1)</button>
74+
<button onClick={() => dispatch({ type: 'DECREMENT' })}>Decrement (-1)</button>
75+
<button onClick={() => dispatch({ type: 'DOUBLE' })}>Double Value</button>
76+
<button onClick={() => dispatch({ type: 'ADD', payload: 5 })}>Add 5</button>
77+
<button onClick={() => dispatch({ type: 'RESET' })}>Reset</button>
78+
</div>
79+
80+
<div className='counter-info'>
81+
<h4>Last Action: {state.lastAction}</h4>
82+
<h4>History:</h4>
83+
<div className='history-list'>
84+
{state.history.map((value, index) => (
85+
<span key={index}>
86+
{value}
87+
{index < state.history.length - 1 ? ' → ' : ''}
88+
</span>
89+
))}
90+
</div>
91+
</div>
92+
</div>
93+
);
94+
}
95+
96+
export default FunctionalReducerCounter;

demo-app/src/client/Router.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ import Board from './Components/Board';
77
import Home from './Components/Home';
88
import Buttons from './Components/Buttons';
99
import ReducerCounter from './Components/ReducerCounter';
10+
import FunctionalReducerCounter from './Components/FunctionalReducerCounter';
1011
// import ButtonsWithMoreHooks from './Components/ButtonsWithMoreHooks';
1112

1213
const domNode = document.getElementById('root');
1314
const root = createRoot(domNode);
1415

16+
const CounterPage = () => (
17+
<div style={{ display: 'flex', justifyContent: 'center', gap: '2rem', flexWrap: 'wrap' }}>
18+
<ReducerCounter key='ReducerCounter' />
19+
<FunctionalReducerCounter key='FunctionalReducerCounter' />
20+
</div>
21+
);
22+
1523
root.render(
1624
<BrowserRouter key='BrowserRouter'>
1725
<Nav key='Nav' />
@@ -22,7 +30,7 @@ root.render(
2230
the public facing Buttons page and the fiber node hooks research page "ButtonsWithMoreHooks" */}
2331
<Route path='/buttons' element={<Buttons key='Buttons' />} />
2432
{/* <Route path='/buttons' element={<ButtonsWithMoreHooks key='ButtonsWithMoreHooks'/>} /> */}
25-
<Route path='/reducer' element={<ReducerCounter key='ReducerCounter' />} />
33+
<Route path='/reducer' element={<CounterPage key='CounterPage' />} />
2634
</Routes>
2735
</BrowserRouter>,
2836

0 commit comments

Comments
 (0)