I’ve been playing with React Server Components for nearly 2 weeks now and this question has been making my brain hurt: how do you provide context to server components? Like for example, providing a theme (e.g dark | light mode) ? Ive checked the Nextjs docs so many times and ive read the same thing like as if it would change. It states that only the client components can useContext. So how would server components useContext if it needs data thats not server based? I can’t fetch data thats state based. Has anyone figured a way around this?
<html>
<body className="page-container">
<ThemeProvider>
{children} // Combination of Server and Client components
</ThemeProvider>
</body>
</html>
export default async function ServerComponent(){
...
const theme = useContext(ThemeContext) // Would give an error; not a client component
...
return (
<div className={theme}>
...
</div>
)
}
I've done everything that was said in the Next.js beta docs: place the provider in a client component and let the server components be the children of said client component. Now, I'm just wondering how to consume the context in server components; it works in client components as they're allowed to use hooks, but not for server components.