(state: State, action: Action)
| 10 | } |
| 11 | |
| 12 | function reducer(state: State, action: Action) { |
| 13 | const { type } = action; |
| 14 | |
| 15 | switch (type) { |
| 16 | case 'increment': { |
| 17 | const newCount = state.count + 1; |
| 18 | const hasError = newCount > 5; |
| 19 | return { |
| 20 | ...state, |
| 21 | count: hasError ? state.count : newCount, |
| 22 | error: hasError ? 'Maximum reached' : null, |
| 23 | }; |
| 24 | } |
| 25 | case 'decrement': { |
| 26 | const newCount = state.count - 1; |
| 27 | const hasError = newCount < 0; |
| 28 | return { |
| 29 | ...state, |
| 30 | count: hasError ? state.count : newCount, |
| 31 | error: hasError ? 'Minimum reached' : null, |
| 32 | }; |
| 33 | } |
| 34 | default: |
| 35 | return state; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export default function Demo() { |
| 40 | const [state, dispatch] = useReducer(reducer, { |
nothing calls this directly
no outgoing calls
no test coverage detected