Next.js Discord

Discord Forum

DOMContentLoaded and NextJS

Answered
Gharial posted this in #help-forum
Open in Discord
GharialOP
Hello guys I am trying to use the even DOMContentLoaded with NextJS but it does not seem like I can, I tried using the Script tag but it did not work, any idea why ? I am using the app directory
Answered by Gharial
I know it is recommended to use useEffect to not block the thread but blocking the thread is actually what I need so I can do actions before anything is painted on the screen
View full answer

9 Replies

GharialOP
and yes I really do need DOMContentLoaded and not useEffect
I need it to be before component mounts kind of
if I am not wrong useEffect runs when it mounts but I can get DOMContentLoaded before it mounts. right ?
<Script id="show-banner">
                {`document.addEventListener('DOMContentLoaded', () => {
                console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
                const x = document.querySelector('#exa-shape')

                console.log(x)
            })

            console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
            console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
            console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
            console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')
            console.log('HIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII')`}
            </Script>
thats basically what I triedand only the HIIIIIIIIII console logs are showing
GharialOP
any idea ? I am still in need of help
Border Terrier
But useffect and domcontentloaded run after component mounts
GharialOP
yeaaaa, well I found another solution since this one did not work
I used useEffectLayout()
GharialOP
I know it is recommended to use useEffect to not block the thread but blocking the thread is actually what I need so I can do actions before anything is painted on the screen
Answer