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.

Library does not work with containers that are not initially rendered within a component (React)

See original GitHub issue

Hey there! Great job with this library, it’s such a quick and easy way to improve UX. I encountered an issue where the animations do not work when a container is not initially rendered in the DOM. I created a small codesandbox to demonstrate this here:

Edit distracted-blackburn-kux9pj

Notice parent2 animates as expected, however its children do not. If you change the initial state of showParent2 to true, everything will animate correctly (until parent2 isn’t being rendered anymore). An obvious workaround to this issue is to compose your app in a way such that each individual ref is always rendered within each component.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mtsknncommented, Jun 1, 2022

Good thoughts!

I had a good night’s sleep and realized that a callback ref is all that’s needed, and it also doesn’t cause extra re-renders: demo 3. Hooray! Not sure if the useCallback is even necessary here. Maybe not because there’s no state to trigger possibly infinite re-render loops. But it shouldn’t hurt either to have it.

(Edit: if useCallback is used, options should be included in the dependency array. I updated the demo link.)

(Edit 2: updated the demo again with a “toggle duration” button. The current useAutoAnimate hook doesn’t support changing the options on the fly because options is not included in the useEffect’s dependency array, right? So using a callback ref would fix this as well. 😀 Though the current hook would be easy to fix as well, just include options in the dep array.)

By the way, the current useAutoAnimate hook has a useEffect. Is there a need to return a clean-up function, or is there anything to clean up manually after using AutoAnimate? If there is, I’m not sure if the callback ref supports that easily.

1reaction
justin-schroedercommented, May 31, 2022

@mtsknn This is an interesting topic, and while its true that one or two additional renders might not be a huge deal, it does seem there could be unintentional runaway side effects using DOM elements as state, possibly even infinite loops. I’ve always avoided putting Nodes into state because it feels like an anti-pattern, but I’m certainly not prepared to articulate it better than that and your examples are compelling, so I’m happy to concede this if there are a quorum of people that think it is an acceptable path forward.

However, the MutationObserver AutoAnimate uses is capable of deep observation, we intentionally leave this disabled to prevent really deep node trees having a ton of tracking on them (technically this issue should be marked as “working as intended”, but I’m leaving it open as I agree the DX should be improved). Perhaps deep observation should be an opt-in behavior people can judiciously add to their projects when it makes sense to do so.

I’d also love to hear from other React devs what their opinions are on putting DOM nodes in state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Integrating with Other Libraries - React
We will implement it as an uncontrolled component for simplicity. First, we will create an empty component with a render() method where we...
Read more >
React testing library - check the existence of empty div
When I call debug() in my test, it shows a <div /> . How do I check that the component is returning an...
Read more >
API | Testing Library
body and this is where your React component will be rendered. If you provide your own HTMLElement container via this option, it will...
Read more >
5 things not to do when building React applications
In SSR, the JavaScript content initially renders from the server. After the initial render, the client-side script takes over and works like a ......
Read more >
The React Mega-Tutorial, Chapter 3: Working with Components
You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks...
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