Created
June 23, 2020 12:04
-
-
Save omar2205/0f585a18df3da15f773e1a43dbe02a04 to your computer and use it in GitHub Desktop.
react useReducer quick
This file contains 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
const initialState = { | |
counter: 0 | |
} | |
const counterReducer= (state, event) => { | |
if (event.type === 'INC') { | |
return { | |
...state, | |
counter: state.counter +1 | |
} | |
} | |
if (event.type === 'DEC') { | |
return { | |
...state, | |
counter: state.counter -1 | |
} | |
} | |
} | |
const App = () => { | |
const [state, dispatch] = React.useReducer(counterReducer, initialState) | |
return ( | |
<div> | |
<h1>{state.counter}</h1> | |
<p onClick={() => dispatch({type: 'INC'})}> + </p> | |
<p onClick={() => dispatch({type: 'DEC'})}> - </p> | |
</div> | |
) | |
} | |
const el = document.getElementById('app') | |
ReactDOM.render(<App />, el) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment