Next.js Discord

Discord Forum

How does next-themes achieve seamless local storage handling for Initial state?

Answered
von Frich posted this in #help-forum
Open in Discord
I'm curious about how next-themes smoothly retrieves theme preferences from local storage on initial load, is it achieved by using a context? I'm trying to replicate a similar effect for managing sidebar open/closed states, akin to the setup on the ChatGPT website. Can anyone shed light on how next-themes accomplishes this feat? Thanks!

5 Replies

Answer
Plott Hound
It’s messy but it works
@Plott Hound It’s messy but it works
Thanks, was hoping there was a more elegant way of solving it though🫠
@von Frich Thanks, was hoping there was a more elegant way of solving it though🫠
Plott Hound
no problem. yeah its a pain point with SSR since the client actually needs to mount to be able to access local storage
yea as canvasguy explained, this is a fundamental problem with server side rendering. you could use cookies instead of localStorage but then to avoid flashes you would need to render your page at request time which poses yet more unwanted problems.