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.

UseLayoutEffect inconsistent behavior

See original GitHub issue

Assuming the useLayoutEffect should fire after DOM is painted, there’s a bug where the elements are not yet flushed to DOM for the initial invocation of useLayoutEffect when it’s used in a component that’s rendered as a nested child:

Case 1:

return <CompoUsingLayoutEffect/>

Case 2:

return <div>
	<CompoUsingLayoutEffect/>
</div>

In case 2, the useLayoutEffect is initially fired before the DOM is painted. While in case 1, it works as expected.

Consecutive executions of useLayoutEffect are called after the DOM is painted.

Best if you check the repro yourself:

Preact: https://codesandbox.io/s/agitated-morse-b6zrp?expanddevtools=1 React: https://codesandbox.io/s/hardcore-hertz-p5up6?expanddevtools=1

In the repros above, all textareas should be initially resized to fit the content, but in Preact the first textarea is initially set to height: 0 because the scrollHeight is read before the textarea is in the DOM (check the console for 0 false which indicates the element isn’t in the document).

Preact 10.0.0-rc.1

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
andrewigginscommented, Oct 20, 2019

FYI, I think #2012 fixes this issue. I tried your text area demo with it and I think it resolved it

1reaction
dwellecommented, Oct 20, 2019

Yes, good point about useEffect.

My misconceptions around these things mainly arise from the fact (as I’ve edited the comment above just when you posted your reply) that I don’t know how event loop frames relate to animation frames (setTimeout vs requestAnimationFrame), and what React actually uses to schedule useEffect (I tried looking into React source, but there’s an insane amount of abstractions, coupled with Flow, and it’s unreadable to me).

Read more comments on GitHub >

github_iconTop Results From Across the Web

React useLayoutEffect vs. useEffect with examples
3. Inconsistent visual changes · useEffect , you get a flicker before the DOM changes are painted, which was related to how refs...
Read more >
useEffect vs useLayoutEffect: the difference and when to use ...
Finding a use case for useLayoutEffect seems like a very hard task. ... or visual inconsistency then you need to use useLayoutEffect ......
Read more >
React useLayoutEffect() - Hook API Reference In React
When dealing with inconsistencies in visual changes, useLayoutEffect shines. You get a flicker with useEffect even before DOM changes are drawn, ...
Read more >
React Hooks showing inconsistent behaviour - Stack Overflow
I wanted to get a feel for react hooks and was trying them out by making a normal todo list with adding and...
Read more >
Hooks API Reference - React
You can replicate this behavior by combining the function updater form with object spread ... React provides one additional Hook called useLayoutEffect ....
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