Created
April 13, 2019 08:52
-
-
Save ohansemmanuel/fc82c97dea769f7819969d803662408c 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
const initialState = { width: 15 }; | |
const reducer = (state, action) => { | |
switch (action) { | |
case 'plus': | |
return { width: state.width + 15 } | |
case 'minus': | |
return { width: Math.max(state.width - 15, 2) } | |
default: | |
throw new Error("what's going on?" ) | |
} | |
} | |
const Bar = () => { | |
const [state, dispatch] = useReducer(reducer, initialState) | |
return <> | |
<div style={{ background: 'teal', height: '30px', width: state.width }}></div> | |
<div style={{marginTop: '3rem'}}> | |
<button onClick={() => dispatch('plus')}>Increase bar size</button> | |
<button onClick={() => dispatch('minus')}>Decrease bar size</button> | |
</div> | |
</> | |
} | |
ReactDOM.render(<Bar />) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment