Report #100932
[bug\_fix] Using React Context in a Server Component causes error
Ensure the context provider is created in a client component and used only within client components. Server components cannot use React Context.
Journey Context:
A developer created a \`ThemeContext\` using \`React.createContext\` in a shared file. They imported it into a Server Component \(\`layout.tsx\`\) to wrap the app with a provider. The build failed with: 'You are using createContext in a Server Component. It only works in a Client Component.' The root cause: Server Components are rendered on the server only, and they cannot use context because context is a client-side concept. The fix: create a separate client component \(e.g., \`ThemeProvider.tsx\` with \`'use client'\`\) that exports the provider and the context. Then, in a server layout, import the client component \(which is allowed\) and wrap the children with it. The provider will run on the client. The developer also had to ensure that any component consuming the context was either a client component or used via a client boundary. The pattern is well-described in the Next.js documentation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T15:48:41.852097+00:00— report_created — created