Code Monkey home page Code Monkey logo

Comments (22)

vzaidman avatar vzaidman commented on May 22, 2024 1

this guy built a whole library around it:
https://github.com/alexreardon/use-memo-one

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024 1

@Code-guru @Hypnosphi
released a fix in version v5.0.0-rc.1

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

removed useMemo from being concidered as a reason for re-renders

from why-did-you-render.

o0x2a avatar o0x2a commented on May 22, 2024

It seems like this issue is still present, here is one for useMemo even if has empty [] dependencies list.
Screenshot 2020-10-13 at 16 58 22

from why-did-you-render.

o0x2a avatar o0x2a commented on May 22, 2024

Can you confirm this issue? Should this ticket re-opened? @vzaidman

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

are you using the latest version?

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

@Hypnosphi hey!
do you remember why we did this?
Who do we even care if useMemo/useCallback returns a deep equals result?

from why-did-you-render.

o0x2a avatar o0x2a commented on May 22, 2024

I use v4.3.2, the useMemo is used inside own custom hook, and the custom hook is not tracked by wdyr.

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

@vzaidman because we will most likely pass it as a prop after all

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

But I agree that those shouldn't be considered reasons for rerender of the component that uses them. I think we can only update dependenciesMap in them, without tracking hook changes

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

To be fair, useMemo already was there at the moment of #147

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

OK! I just wanted to make sure.

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

@Hypnosphi now i also see that

dependenciesMap.set(hookResult, get(args, dependenciesPath))

saves the deps of the hook per hook result.

but if a hook simply returns an object that was created anywhere else it might create false positives.

for this reason I will only keep useCallback and remove useMemo from participating in dependenciesMap.

it's rare to return functions from useMemo anyway...

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

not so rare:

const throttledCallback = useMemo(() => throttle(callback, timeout), [callback, timeout])

Please don't break this usecase.

What are usecases for returning a function created somewhere else by the way? Why does one even need useMemo for that?

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

Also, dependenciesMap can't create false positives, only false negatives: it marks some functions as deeply different

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

gotcha i'll keep it inside.

By the way the throttle example is an anti pattern based on React docs:

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

What is the recommended way to do that?

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

i think

const [throttledCallback] = useState(() => throttle(callback, timeout))

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

check out https://twitter.com/0xca0a/status/1314326386555924480

from why-did-you-render.

Hypnosphi avatar Hypnosphi commented on May 22, 2024

What if both callback and timeout are dynamic?

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

Probably a longer and uglier hook using useRef...

from why-did-you-render.

vzaidman avatar vzaidman commented on May 22, 2024

i'll release version 5 very soon.

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.