Reading cookies in Client Components during server-rendering with React's use()
hook (Next.js 15)
#79665
Replies: 4 comments 3 replies
-
Hey! 👋 Really interesting question — I explored this too. ❗ TL;DR: Using
|
Beta Was this translation helpful? Give feedback.
-
@icyJoseph could you advise please? |
Beta Was this translation helpful? Give feedback.
-
I'm doing the same here... didn't had any problem yet. Just a side note: React.use isn't a hook, it means it don't follow the hooks rules (for conditional ones, looping, respecting call order, etc.), as you already should know by using it inside an if. But when you name your isomorphic function as Just name it |
Beta Was this translation helpful? Give feedback.
-
Hey! I’ve been experimenting with this too — here’s my take: export default async function Page() {
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Context
With Next.js 15,
cookies()
is now async. I need to read cookies in Client Components during their server-side rendering phase. I've implemented this pattern using React'suse()
hook:Then we would just render this component in a server component page.
use()
to handle async cookies in Client Components during SSR considered an anti-pattern?My goal is to have a better UX during reloads and phase rendering transitions. For example, I have a cookie that saves the state of the sidebar menu (collapsed or expanded). During a reload, I want to ensure that it remains in the last state during both server and client renders of a client component.
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions