Functional refs still behave slightly different from React
See original GitHub issueThis is a follow-up issue to #2052, which was fixed by #2055.
While re-testing the sandbox from that issue, I found that there is still a subtle deviation from the order of events in React - here’s the console output for comparison:
As you can see, React cleans up all refs before creating new ones - whereas Preact seems to create and clean up refs in the order they were created?
This could be important and might create problems for components that use several functional refs to manipulate the same component or global state.
(Refs are sometimes used as a way to create side-effects that depend on the life-cycle of elements - as opposed to useEffect
or life-cycle methods, which depend on the life-cycle of components.)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Functional refs behave different from React #2052 - GitHub
It's intensional because it's a new function identity we don't know if it's a different ref all together. ... If it's the same...
Read more >A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >Forwarding Refs - React
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary...
Read more >Everything You Need to Know About Refs in React
Short for “reference”, refs are a way to access underlying DOM elements in a React component. There are many reasons why you would...
Read more >React Functional Components: In-Depth Guide - KnowledgeHut
Refs are a function that use to access the DOM from components. You only need to attach a ref to the element in...
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
Looking at the difference in behavior I guess it comes down to different diffing strategies used under the hood. React has a 2-pass rendering algorithm and for that it makes sense to flush all pending refs in one go. We currently do have a single-pass algo and update elements as we go. We offload some effects into queues to ensure that effects are called independent of that. In the long run we’ll likely switch to a 2-pass renderer, but it will take time.
This is a deviation that was outlined in the PR and probably won’t be fixed since it does not cause any severe issues