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.

Functional refs still behave slightly different from React

See original GitHub issue

This 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:

image

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
marvinhagemeistercommented, Nov 12, 2019

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.

2reactions
JoviDeCroockcommented, Nov 12, 2019

This is a deviation that was outlined in the PR and probably won’t be fixed since it does not cause any severe issues

Read more comments on GitHub >

github_iconTop 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 >

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