Non referentially stable refs cause multiple updates
See original GitHub issueWhen a React element receives a different ref
on re-renderer the previous ref
is updated to null
and the new one is given the current value — regardless of changes in value.
I made a small CodeSandbox to verify this. In it you can also find a “solution”, which is to transform the API into an hook. This let us introduce useCallback
to give the user a referentially stable ref.
This doesn’t solve the problem completely: if one of the refs changes all other refs receive a double (and useless) update too.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:30 (3 by maintainers)
Top Results From Across the Web
Non referentially stable refs cause multiple updates #5 - GitHub
When a React element receives a different ref on re-renderer the previous ref is updated to null and the new one is given...
Read more >How can I use multiple refs for an array of elements with hooks?
Accessing an undefined ref is not possible. A ref is initialized upon the first read. After that, it remains referentially stable.
Read more >You're overusing useMemo: Rethinking Hooks memoization
In some cases, useMemo is irrelevant, overused, and harmful to your application performance. Learn these situations and how to avoid them.
Read more >Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >Jetpack Compose Stability Explained | Android Developers
Compose determines the stability of each parameter of your composables to work out if it can be skipped or not during recomposition.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
MUI’s version is stable, but only in a single specific sense, and formally fragile. Let me explain.
A perfect ref merger should:
MUI’s current implementation has the following problems:
useMemo
which as we all know as historically been defined as an optimization tool, and should not be used to have “singleton” semantics. In this case avoiding recreating a ref is some kind of an optimization trick, but since ref updating is in the imperative world (a function ref can have side effects [citation needed]), we need more guarantees.I don’t think this needs effects at all. Doesn’t this work to preserve the resulting callback ref?
The reason for disabling react-hooks/exhaustive-deps is that we’re passing the raw refs array to
useCallback
, which means that eslint can’t statically check it. But it works fine for the actual checking which preserves and updates the callback.Also, the
Object.isFrozen
test shows up for me in tests. I’m not sure under what conditions the ref becomes a frozen, empty object, but we just need to avoid assigning in that case.