Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:
- throw new Error();
- }
- }
- function Counter() {
- const [state, dispatch] = useReducer(reducer, initialState);
- return (
- <>
- Count: {state.count}
- <button onClick={() => dispatch({type: 'increment'})}>+</button>
- <button onClick={() => dispatch({type: 'decrement'})}>-</button>
- </>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement