Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. const initialState = { width: 15 };
  2.  
  3. const reducer = (state, action) => {
  4. switch (action) {
  5. case 'plus':
  6. return { width: state.width + 15 }
  7. case 'minus':
  8. return { width: Math.max(state.width - 15, 2) }
  9. default:
  10. throw new Error("what's going on?" )
  11. }
  12. }
  13.  
  14. const Bar = () => {
  15. const [state, dispatch] = useReducer(reducer, initialState)
  16. return <>
  17. <div style={{ background: 'teal', height: '30px', width: state.width }}></div>
  18. <div style={{marginTop: '3rem'}}>
  19. <button onClick={() => dispatch('plus')}>Increase bar size</button>
  20. <button onClick={() => dispatch('minus')}>Decrease bar size</button>
  21. </div>
  22. </>
  23. }
  24.  
  25. ReactDOM.render(<Bar />)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement