TanStackT
TanStack6mo ago
30 replies
nursing-lime

Client-only router context

With TanStack Router (without Start) we can pass context from a React Context by putting the <RouterProvider> inside the React Context provider and passing the context prop. https://tanstack.com/router/latest/docs/framework/react/guide/router-context#how-about-using-react-contexthooks

But in TanStack Start there is <StartClient> instead of <RouterProvider> and it doesn't take context.

My use case right now is in migrating a Router app to Start so that we can get some dynamic SSR injection into the root route but have all other routes disable SSR with the new selective SSR. Our app's loaders can't be isomorphic so what I am doing now is creating a layout under the root route to initialize the client-side context values but these get recreated every time that
beforeLoad
runs which is every navigation. This is different than the Router case where we would initialize the router with those context values so the initialization wouldn't be in a loader.

I might be missing an obvious pattern to achieve what I need. I'm sure there is a way to also just have client-only initialization and access those in client-only loaders such as initializing them in client.tsx but it feels less "elegant" than the context pattern in Router.
Was this page helpful?