Created
February 11, 2021 19:13
-
-
Save matheusml/28fc3a9f1142765c7f3d0c514f9f9738 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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