T
TanStack2mo ago
conscious-sapphire

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.
8 Replies
rival-black
rival-black2mo ago
can you please provide a complete project?
conscious-sapphire
conscious-sapphireOP2mo ago
no, it's a private project at work
rival-black
rival-black2mo ago
then create a minimal example based on one of our examples
conscious-sapphire
conscious-sapphireOP2mo 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.
conscious-sapphire
conscious-sapphireOP2mo ago
here you go created a test route it's also happening with it
conscious-sapphire
conscious-sapphireOP2mo ago
No description
conscious-sapphire
conscious-sapphireOP2mo ago
@Manuel Schiller Should I make a GH issue? Not sure if I'm getting something wrong or it's an actual bug.
rival-black
rival-black2mo ago
sure create an issue for that we'll find out either way

Did you find this page helpful?