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.

is passing a ref to useMemo considered cheating?

See original GitHub issue

I have an object which is created via useMemo

I do not want this object to be recreated every time a certain dependency changes, and yet I want it to always see the latest version of that dependency (not just what it was upon initial creation)

Currently, I’m doing something like this:

//bar changes between renders
const latestFoo = useRef(bar); 

//obj will only be created once, yet it sees the updated latestFoo
const obj = useMemo(() => ({
 doSomething: () => { 
    //use and/or set latestFoo.current 
  }
}), [latestFoo]);

This feels like a little bit of a lie… because it kindof depends on an updated latestFoo.current… not really latestFoo the ref, if that makes sense.

Yet it works fine as far as I can see…

Not sure if I should feel bad about lying to useMemo about its deps, or if it’s not really a lie at all and this is actually a totally expected use case of useRef

Any tips are appreciated.

P.S. this was prompted by refactoring my code after reading this page, which was an enourmous help in understanding hooks and writing code that uses them more consciously: https://overreacted.io/a-complete-guide-to-useeffect/

@gaearon would you rather me post questions like this somewhere else? Didn’t see a comment section on the site and I don’t use twitter…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
gaearoncommented, Mar 20, 2019

Would you distinguish between putting ref vs ref.current in the deps list?

Yes, absolutely. Putting ref in deps list makes sense (but generally you can consider it to always be the same object anyway). Putting ref.current in deps list almost never makes sense (because it’s mutated outside of React data flow). If you’re not sure, the lint rule will help you: https://github.com/facebook/react/issues/14920.

1reaction
gaearoncommented, Mar 20, 2019

In general, ref.current shouldn’t be in the deps list because its whole purpose is to be mutable outside the React data flow.

I think your use case seems fine but maybe adding more details about why you’re doing it (as in, what is foo and bar here) might help discover better ways.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error while trying to use ref inside useMemo() - Stack Overflow
I have a problem of using ref inside useMemo(). I'm getting an error: Cannot read property current of undefined. How to fix this...
Read more >
Demystifying useRef and useMemo in React
It is used by invoking useRef function and passing an initial value to it. First let's look at the syntax and how to...
Read more >
Why hooks are the best thing to happen to React
Arguably the most common React hook, useState helps you pass in state variables in a functional component. Take a look at the code...
Read more >
React - The Missing Parts - Acko.net
No-hooks and Early Return; Component Morphing; useMemo vs useEffect ... fine in vanilla React, as a perfect example of "cheating with refs":
Read more >
A Complete Guide to useEffect - Overreacted
What is [] ? Do I need to specify functions as effect dependencies or not? Why do I sometimes get an infinite refetching...
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 Reddit Thread

No results found

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