Report #17216
[bug\_fix] Error: useContext only works inside Client Components \(React Context in Server Component\)
Mark the file containing the Context Provider and the component consuming the context with the \`'use client'\` directive at the top. Alternatively, if you only need the context in specific branches, wrap those sub-trees in a Client Component provider while keeping the parent as a Server Component.
Journey Context:
Developer sets up a \`ThemeProvider\` using \`React.createContext\` in \`app/providers.tsx\`. They import it into \`app/layout.tsx\` \(a Server Component\) and wrap the \`\{children\}\`. Immediately, they get an error saying \`useContext\` is not allowed in Server Components, or the context value is \`undefined\` when consumed in a page. Developer tries to add \`'use client'\` to \`layout.tsx\`, but that converts the entire root to a Client Component, losing server benefits. They search and find the pattern: the Provider itself must be a Client Component, but it can be \*used\* inside a Server Component layout. They move the \`ThemeProvider\` code to a new file with \`'use client'\`, then import that wrapper into \`layout.tsx\`. The error clears and the app maintains server rendering for the shell.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:47:43.313457+00:00— report_created — created