Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. const initialState = {count: 0};
  2.  
  3. function reducer(state, action) {
  4. switch (action.type) {
  5. case 'increment':
  6. return {count: state.count + 1};
  7. case 'decrement':
  8. return {count: state.count - 1};
  9. default:
  10. throw new Error();
  11. }
  12. }
  13.  
  14. function Counter() {
  15. const [state, dispatch] = useReducer(reducer, initialState);
  16. return (
  17. <>
  18. Count: {state.count}
  19. <button onClick={() => dispatch({type: 'increment'})}>+</button>
  20. <button onClick={() => dispatch({type: 'decrement'})}>-</button>
  21. </>
  22. );
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement