Report #16346
[bug\_fix] React Context value is undefined in Client Components when Provider is in a Server Component
Move the Context Provider into a Client Component \(file with "use client"\) and import that wrapper into the Server Component layout/page. The Provider itself must be a Client Component to hold the context state. Root cause: While Context can be created in a Server Component, the Provider component uses React state to manage context values, which requires client-side React execution. Server Components cannot maintain the mutable state required for context propagation.
Journey Context:
Developer creates a \`ThemeContext\` in \`app/context/ThemeContext.tsx\` using \`createContext\` and a Provider component that uses \`useState\` to manage the theme. They import the \`Provider\` directly into \`app/layout.tsx\` \(the root Server Component\) wrapping the children. The application builds, but at runtime, any Client Component that consumes the context using \`useContext\(ThemeContext\)\` receives \`undefined\` instead of the theme value. Developer debugs by logging the Provider and sees it renders, but the context value is lost. After reading Next.js documentation on Context, they realize that although the Provider looks like a wrapper, it relies on \`useState\` which is a client-only hook. The Provider must be treated as a Client Component. Developer moves the Provider logic into \`app/components/ThemeProvider.tsx\` with "use client" at the top, then imports \`ThemeProvider\` \(not the raw Provider\) into the root layout. The context now propagates correctly because the Client Component boundary allows the context state to persist in the client React tree.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:24:26.992466+00:00— report_created — created