MCPcopy Create free account
hub / github.com/cosdensolutions/code / reducer

Function reducer

videos/long/learn-react-hooks-useReducer/index.tsx:12–37  ·  view source on GitHub ↗
(state: State, action: Action)

Source from the content-addressed store, hash-verified

10}
11
12function 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
39export default function Demo() {
40 const [state, dispatch] = useReducer(reducer, {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected