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.

Set root after mount for SSR

See original GitHub issue

I’m using Next.js and I’d like to set root to a specific element.

window and document are only accessible after mount.

Is there an SSR-friendly way to wait until the component is mounted to set root?

Note: useEffect fires only after mount.

// this won't work; document not defined
const { ref, inView } = useInView({
    root: document.querySelector(".myroot"),
});

useEffect(() => {
    if (inView) {
        doSomething();
    }
}, [inView, doSomething]);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
allan2commented, May 26, 2021

Thanks! I got it working in v1 and v2. I used two observers for v1, one above the target div. When that is hidden but the target is visible, I achieve my goal.

https://f4rwj.sse.codesandbox.io/ https://codesandbox.io/s/competent-cohen-f4rwj?file=/pages/index.tsx

0reactions
thebuildercommented, May 26, 2021

You could use the new Intersectionobserver v2. Support is still limited https://caniuse.com/intersectionobserver-v2

Otherwise it might make sense to look at the scroll position?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change owner of mount point - unix - Server Fault
So mount /var/lib/mysql then chown mysql.mysql /var/lib/mysql . This will change the permissions of the root of the MySQL DB filesystem.
Read more >
Server-Side Rendering (SSR) - Vue.js
Teleports require special handling during SSR. If the rendered app contains Teleports, the teleported content will not be part of the rendered string....
Read more >
NextJS Track Mounted Components in SSR - Stack Overflow
I have dig into this and my feeling is that this is not possible for multiple reason. The setState update is async and...
Read more >
React 18 introduces new root API ( ReactDOM.createRoot )
React 18 adds the new root API that comes up with the new out-of-the-box improvements.
Read more >
A beginner's guide to React Server-Side Rendering (SSR)
In this lesson, we are going to talk about server-side rendering (SSR), its benefits, and its pitfalls. Then we will set up 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