Skip to content

Instantly share code, notes, and snippets.

@matheusml
Created February 11, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matheusml/28fc3a9f1142765c7f3d0c514f9f9738 to your computer and use it in GitHub Desktop.
Save matheusml/28fc3a9f1142765c7f3d0c514f9f9738 to your computer and use it in GitHub Desktop.
import React, { useReducer } from 'react';
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'INCREMENT':
return {count: state.count + 1};
case 'DECREMENT':
return {count: state.count - 1};
default:
return state;
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'DECREMENT'})}>-</button>
<button onClick={() => dispatch({type: 'INCREMENT'})}>+</button>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment