#How to avoid hydration errors with conditional rendering based on auth state?

9 messages · Page 1 of 1 (latest)

narrow hearth
#

Hello!

I am getting a hydration error because my auth state does not line up on the server and the client. I have posted my component. Basically my auth function really only gets the session on the client leading to this rendering to be out of sync between server/client

how would i fix something like this?

#

line 35 is where i conditionally render the dashboard/sign up buttons and line 8 is where i'm getting the auth state from

#

i guess a deeper question is also how would i go about isomorphically getting the session on the server and the client so that the SSRd page does load in correctly? is that the right approach?

narrow hearth
#

How to avoid hydration errors with conditional rendering based on auth state?

drifting tapir
# narrow hearth i guess a deeper question is also how would i go about isomorphically getting th...

A very common patter is setting the user inside the router context on the server (by returning it from __root.tsx beforeLoad) and use useRouteContext hook to read the value in the navbar, it works in the server and in the client

https://tanstack.com/router/latest/docs/framework/react/guide/router-context

TanStack Router's router context is a very powerful tool that can be used for dependency injection among many other things. Aptly named, the router context is passed through the router and down throug...

narrow hearth
remote stratus
narrow hearth
thin sinew