Next.js Discord

Discord Forum

State Management for SSR pages for User Roles (Next.js 13)

Unanswered
Carpenter wasp posted this in #help-forum
Open in Discord
Carpenter waspOP
Hey there!

I am new to Next.js and React in general. I am trying to create website where different components can access user data like username, profile pic, etc. For example I want admins to have extra buttons on the page normal users dont have, and it would be great if those components could read the user data somehow. React’s use and create context seem to be a good way to handle it. But as far as I know, using it makes all its children CSR instead of SSR (since i'd put it in the layout.tsx file). Is there a better state management thing I could use?

3 Replies

So you want to keep it all pre-rendered on the server? If you're getting user data via cookies or headers, the page will be rendered at request time.
(since dynamic functions will opt your page into dynamic rendering)
CSR only happens when you're using hooks like useEffect and useState, you can pass data via Context API, it won't make everything a client component, but the component that receives the context must be a client component.
So Context API doesn't make its children a client component