Next.js Discord

Discord Forum

Theme flickering

Answered
Mawhadmd posted this in #help-forum
Open in Discord
Why is there theme flickering whenever i refresh the page? I use useEffect and data-theme, but it's never occurred to me

5 Replies

@Mawhadmd Why is there theme flickering whenever i refresh the page? I use useEffect and data-theme, but it's never occurred to me
because useEffect is not run immediately, it is only run after hydration.

you need to use an inline <script> so that it is run immediately and doesn't have to wait for hydration.

or just use https://github.com/pacocoursey/next-themes which streamlines everything for you
@joulev because useEffect is not run immediately, it is only run after hydration. you need to use an inline <script> so that it is run immediately and doesn't have to wait for hydration. or just use <https://github.com/pacocoursey/next-themes> which streamlines everything for you
yeah and that's what causing me to freak out, when i first built the app i used useEffect and it worked impeccably, but now when i try to use the hook again it's flickering for some reason... Using the script caused hydration error too, i'm basically stuck
Answer
no, it is fine, this hydration mismatch is expected hence suppressing it is fine