question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useMemo / useCallback cache busting opt out

See original GitHub issue

According to the React docs, useMemo and useCallback are subject to cache purging:

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. source

I am working on moving react-beautiful-dnd over to using hooks https://github.com/atlassian/react-beautiful-dnd/issues/871. I have the whole thing working and tested 👍

It leans quite heavily on useMemo and useCallback right now. If the memoization cache for is cleared for a dragging item, the result will be a cancelled drag. This is not good.

My understanding is that useMemo and useCallback are currently not subject to cache purging based on this language:

In the future, React may choose to “forget”

Request 1: Is it possible to opt-out of this cache purging? Perhaps a third options argument to useMemo and useCallback:

const value = useMemo(() => ({ hello: 'world' }), [], { usePersistantCache: true });

(Naming up for grabs, but this is just the big idea)

A work around is to use a custom memoization toolset such as a useMemoOne which reimplements useMemo and useCallback just using refs see example

I am keen to avoid the work around if possible.

Request 2: While request 1 is favourable, it would be good to know the exact conditions in which the memoization caches are purged

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:37
  • Comments:36 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
bhovhannescommented, Apr 10, 2019

@alexreardon useCallback docs do not say that it is subject to cache purging. Docs are not clear enough here though.

Docs only say that:

useCallback(fn, deps) is equivalent to useMemo(() => fn, deps)

If useCallback is equivalent to useMemo, why we have useCallback at all? I assume if there is a separate useCallback hook shipped with React there is a reason for that and that reason is not documented well.

9reactions
awearycommented, Apr 1, 2019

I think this is an uncommon enough case that using useRef to implement your own stable references is the right approach.

While request 1 is favourable, it would be good to know the exact conditions in which the memoization caches are purged

I don’t think it’s valuable to document the internal caching strategy beyond “React might clear the cache when it needs to” since it’s likely to be dynamic and difficult to predict. No single component could predict cache hits or misses at runtime with any certainty.

It does a side effect during the render

Just a heads up that this is likely to be problematic in concurrent mode, since the function might be called many times with different props.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding useMemo and useCallback - Josh W Comeau
useMemo is essentially like a lil' cache, and the dependencies are the cache invalidation strategy. In this case, we're essentially saying “ ...
Read more >
Can we rely on useCallback as a semantic guarantee? | Trabe
We haven't found any answer from the React team ensuring that the cache busting just affects useMemo , not useCallback . The only...
Read more >
How to useMemo and useCallback: you can remove most of ...
If we're using useMemo , during the initial render React needs to cache the result value - that takes time. Yes, it will...
Read more >
useMemo (possible?) cache purging and useEffect dependency
In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for...
Read more >
useMemo - React Docs
useMemo is a React Hook that lets you cache the result of a calculation between re-renders. const cachedValue = useMemo(calculateValue, dependencies).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found