Giter VIP home page Giter VIP logo

Comments (2)

dai-shi avatar dai-shi commented on July 3, 2024 1

Hi, nice to know that you read this very deeply.
Your assumption is correct. By "almost perfect", it means there are two subtle differences. Especially, the second one is important. (The first one is really subtle, because JS engine optimizes inline functions.)
The second one is what I call the cheat mode, which is described more in detail in this article: https://overreacted.io/a-complete-guide-to-useeffect/#why-usereducer-is-the-cheat-mode-of-hooks
This behavior is used in use-context-selector v1.3, which is used by react-tracked.

from micro-state-management-with-react-hooks.

penx avatar penx commented on July 3, 2024

Hi, nice to know that you read this very deeply.

Thanks! The book's great, I'm on chapter 8 now.

I wrote this a couple of years ago and have been meaning to revisit it.

https://medium.com/@penx/a-usestate-and-context-api-pattern-that-prevents-renders-c3ac847cdf48

The second one is what I call the cheat mode, which is described more in detail in this article: https://overreacted.io/a-complete-guide-to-useeffect/#why-usereducer-is-the-cheat-mode-of-hooks
This behavior is used in use-context-selector v1.3, which is used by react-tracked.

Ah I see now, thanks for taking the time to explain.

In case it's of any use to anyone else, I wrote this example to show how they differ - the bonus is incremented before React's useReducer's dispatch but after the custom userReducer's dispatch:

import { useState, useReducer, useCallback } from "react";

const useReducerCustom = (reducer, initialArg, init) => {
  const [state, setState] = useState(
    init ? () => init(initialArg) : initialArg
  );
  const dispatch = useCallback(
    (action) => setState((prev) => reducer(prev, action)),
    [reducer]
  );
  return [state, dispatch];
};

const init = (count) => ({ count });

const ComponentWithUseReducer = ({ initialCount }) => {
  const [bonus, setBonus] = useState(0);
  const [state, dispatch] = useReducer(
    (prev, delta) => ({ ...prev, count: prev.count + delta + bonus }),
    initialCount,
    init
  );
  const [stateCustom, dispatchCustom] = useReducerCustom(
    (prev, delta) => ({ ...prev, count: prev.count + delta + bonus }),
    initialCount,
    init
  );
  return (
    <>
      <div>Score with useReducer: {state.count}</div>
      <div>Score with custom useReducer: {stateCustom.count}</div>
      <div>
        <button
          onClick={() => {
            setBonus((x) => x + 1);
            dispatch(1);
            dispatchCustom(1);
          }}
        >
          +1
        </button>
      </div>
    </>
  );
};

export default function App() {
  return <ComponentWithUseReducer initialCount={0} />;
}

https://codesandbox.io/s/custom-use-reducer-with-date-808kkz?file=/src/App.js:0-1248

from micro-state-management-with-react-hooks.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.