Giter Site home page Giter Site logo

Comments (10)

vzaidman avatar vzaidman commented on May 10, 2024 2

fixed in v5.0.0-alpha.2

from why-did-you-render.

vzaidman avatar vzaidman commented on May 10, 2024 1

after investigating this issue I believe I have the answers.

When you mark a "huge" component with "whyDidYouRender", you expect it to not re-render where it can be prevented.

While React.useCallback is a very useful hook, it might cause problems nevertheless.

For example (i created a sandbox for this):
https://codesandbox.io/s/welldone-softwarewhy-did-you-render-usecallback-example-7co06

const UseCallbackNewFnOnClick = () => {
  const [value, setValue] = React.useState(0);

  // this useCallback returns a new function on each value change
  const handleClick = React.useCallback(() => {
    setValue(value + 1);
  }, [value, setValue]);

  return (
    <div>
      <h1>useCallback</h1>
      value: {value}
      <br />
      <HugeComponentWithButtonWeDontWantToReRender
        text="increase"
        onClick={handleClick}
      />
    </div>
  );
};

vs

const UseCallbackAlwaysSameFn = () => {
  const [value, setValue] = React.useState(0);

  // this useCallback never returns a new function
  const handleClick = React.useCallback(() => {
    setValue(value => value + 1);
  }, [setValue]);

  return (
    <div>
      <h1>useCallback</h1>
      value: {value}
      <br />
      <HugeComponentWithButtonWeDontWantToReRender
        text="increase"
        onClick={handleClick}
      />
    </div>
  );
};

If you mark a component with whyDidYouRender, it probably means you don't want it to receive a new fn all the time. this can be prevented in many ways. one of them is the way displayed in the example, another one would be not to use hooks at all.

what do you think @DaleSalcedo ?

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 10, 2024 1

Related: facebook/react#14099

from why-did-you-render.

vzaidman avatar vzaidman commented on May 10, 2024 1

yes there are. I still think about how to make it as clear and as robust as possible in this sense. if you have any suggestions, i'll be glad to hear.

from why-did-you-render.

vzaidman avatar vzaidman commented on May 10, 2024 1

Re-opened for the record because I was asked about it again:

We wait on facebook/react#14099 to be fixed so the fn returned from useCallback wan't cause a re-render of the component it is passed to.

from why-did-you-render.

vzaidman avatar vzaidman commented on May 10, 2024

This comparison was created before hooks were available, where passing () => to big components meant trouble. Now it's not the case however:
useCallback regenerates the fn passed to a component only if it changed.

It's indeed something that we need to rethink.

from why-did-you-render.

vzaidman avatar vzaidman commented on May 10, 2024

Related: facebook/react#14099

exectly

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 10, 2024

Possible workaround: facebook/react#14099 (comment)

// use this instead of `useCallback`
function useEventCallback(fn) {
  let ref = useRef();
  useEffect(() => {
    ref.current = fn;
  });
  return useCallback((...args) => ref.current(...args), []);
}

from why-did-you-render.

DaleSalcedo avatar DaleSalcedo commented on May 10, 2024

@vzaidman yes that makes alot of sense. thank you for the example as well.

i should probably close this issue now, thanks everyone for the feedback!

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 10, 2024

@vzaidman there are valid cases for callback invalidation, see facebook/react#14099 (comment)

How can I tell why-did-you-render that callback reference change is expected here?

from why-did-you-render.

Related Issues (20)

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.