T
TanStack5h ago
fascinating-indigo

Cannot make react context work during SSR

I made a simple ThemeContext, I have a providers file which is loading all the providers.
function RootDocument({ children }: { children: ReactNode }) {
return (
<html
lang="en"
>
<head>
<HeadContent />
</head>
<body>
<AppProviders>
<div className="flex min-h-screen flex-col">
<AppHeader />
<main className="flex-1">{children}</main>
<TanStackDevtools
config={{
position: 'bottom-left',
}}
plugins={[
debugControlsDevtoolsPlugin,
{
name: 'Tanstack Router',
render: <TanStackRouterDevtoolsPanel />,
},
]}
/>
</div>
</AppProviders>
<Scripts />
</body>
</html>
)
}
function RootDocument({ children }: { children: ReactNode }) {
return (
<html
lang="en"
>
<head>
<HeadContent />
</head>
<body>
<AppProviders>
<div className="flex min-h-screen flex-col">
<AppHeader />
<main className="flex-1">{children}</main>
<TanStackDevtools
config={{
position: 'bottom-left',
}}
plugins={[
debugControlsDevtoolsPlugin,
{
name: 'Tanstack Router',
render: <TanStackRouterDevtoolsPanel />,
},
]}
/>
</div>
</AppProviders>
<Scripts />
</body>
</html>
)
}
In a component I'm trying to call my useTheme hook.
export const useTheme = () => {
const context = use(ThemeContext)
if (!context) {
throw new Error('useTheme must be used within a ThemeContextProvider')
}
return context
}
export const useTheme = () => {
const context = use(ThemeContext)
if (!context) {
throw new Error('useTheme must be used within a ThemeContextProvider')
}
return context
}
I'm getting this error whenever a route is having a call to useTheme on server side, on client side it works. Not sure what I'm missing.
6 Replies
sensitive-blue
sensitive-blue5h ago
can you please provide a complete project?
fascinating-indigo
fascinating-indigoOP5h ago
no, it's a private project at work
sensitive-blue
sensitive-blue4h ago
then create a minimal example based on one of our examples
fascinating-indigo
fascinating-indigoOP4h ago
GitHub
GitHub - wintercounter/repro-ts-start-provider-issue: Created with ...
Created with CodeSandbox. Contribute to wintercounter/repro-ts-start-provider-issue development by creating an account on GitHub.
fascinating-indigo
fascinating-indigoOP4h ago
here you go created a test route it's also happening with it
fascinating-indigo
fascinating-indigoOP4h ago
No description

Did you find this page helpful?