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 teardown runs after new elements are mounted

See original GitHub issue

After moving to Preact X and using hooks I started noticing something weird happening with the useLayoutEffect hook that I was using to store the scroll position when exiting a route.

What I found is that when the teardown function of the hook runs the DOM contains both the old and the new children at the same time (hence my scroll position was being read wrongly).

Here’s an example: https://codesandbox.io/s/zealous-snyder-37ync

When clicking next the component should change the visible component and there’s a console.log of the DOM when the hook runs. You can see that when running the teardown function both the old and the new children are mounted in the DOM.

I’m not sure if this intended behaviour and I’m just using the hook wrong of if it’s working as intended.

EDIT: for reference here is the same thing in react where the problem doesn’t show up. https://codesandbox.io/embed/nostalgic-taussig-m09gx

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewigginscommented, Oct 21, 2019

Thanks for trying it out! I looked it at and, yes, it seems your bug is a new case I hadn’t considered. @JoviDeCroock created a UT for us based on your bug and I added it to my PR so we can play with it. We may check in #2012 as is to fix other issues but we’ll definitely add this as a test case for us to work on in the meantime.

1reaction
d3x7r0commented, Oct 21, 2019

Ok I managed to get it working here and I have bad news: the behavior seems to be the same. When clicking the button the code still sees both the old and new elements at the same time on the dom. 😔

Read more comments on GitHub >

github_iconTop Results From Across the Web

How the useEffect Hook Works (with Examples) - Dave Ceddia
It's the solution to many problems: how to fetch data when a component mounts, how to run code when state changes or when...
Read more >
How can we implement componentWillUnmount using react ...
Nope, useLayoutEffect doesn't really change anything here because the cleanup will still run after a new component is mounted. So the local ...
Read more >
react-dnd-test-backend | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Understanding React's useEffect cleanup function
In fact, after our effect executes, the next scheduled effect is usually ... >useEffect(() => { const controller = new AbortController(); ...
Read more >
Testing useEffect and Redux Hooks using Enzyme - Medium
Testing Component Mount Effect Hook. Even though the useEffect hook is now running with jest-react-hooks-shallow , I immediately ran into a ...
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