showing results for - "immer reducer hook use"
Leonardo
26 Aug 2020
1import React from "react";
2import { useImmerReducer } from "use-immer";
3
4const initialState = { count: 0 };
5
6function reducer(draft, action) {
7  switch (action.type) {
8    case "reset":
9      return initialState;
10    case "increment":
11      return void draft.count++;
12    case "decrement":
13      return void draft.count--;
14  }
15}
16
17function Counter() {
18  const [state, dispatch] = useImmerReducer(reducer, initialState);
19  return (
20    <>
21      Count: {state.count}
22      <button onClick={() => dispatch({ type: "reset" })}>Reset</button>
23      <button onClick={() => dispatch({ type: "increment" })}>+</button>
24      <button onClick={() => dispatch({ type: "decrement" })}>-</button>
25    </>
26  );
27}