Report #11575
[bug\_fix] useContext returns undefined in child components despite Provider wrapping them
Move the Context.Provider into a Client Component file \(marked with 'use client'\) and import that wrapper into the Server Component layout/page.
Journey Context:
Developer creates an \`AuthContext.tsx\` with \`createContext\` and exports an \`AuthProvider\`. In the root \`app/layout.tsx\` \(a Server Component\), they wrap children with \`\`. Child Client Components \(marked with 'use client'\) call \`useContext\(AuthContext\)\` but receive \`undefined\`. Developer verifies context export, checks for multiple React instances \(diamond problem\), tries moving Provider to a different layout segment. After extensive console logging, they realize that Server Components render in Node.js and their JSX output is sent as HTML; the Context Provider created in the Server Component tree exists in that server React tree, but the child Client Components hydrate separately and look for context in the client React tree. The two trees don't share context automatically. The solution is to ensure the Provider is rendered within a Client Component so it exists in the client-side React tree where children can subscribe to it. The fix involves creating \`app/components/Providers.tsx\` with \`"use client"\` that wraps children with all context providers, then importing that into the root \`layout.tsx\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:42:58.423357+00:00— report_created — created