|
| 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; |
0 commit comments