Report #28866
[bug\_fix] useContext only works in Client Components. Add the "use client" directive at the top of the file to use it \(Context in Server Component\)
Move the context consumption \(useContext\) to a Client Component file \(marked with "use client" at the top\) and import that component into your Server Component. Alternatively, if the data comes from a Server Component, pass it as props to the Client Component instead of using context. You cannot use React Context in Server Components because they execute before the client React tree exists.
Journey Context:
A developer creates a ThemeContext to manage dark/light mode across their Next.js App Router application. They create a provider in app/providers.tsx with "use client" and place it in app/layout.tsx. They then try to consume this context in app/page.tsx \(which defaults to a Server Component\) using const theme = useContext\(ThemeContext\). The build or dev server immediately errors, stating that useContext only works in Client Components. The developer is confused because they believe the Provider in layout.tsx should make the context available everywhere. They learn that while the Provider can wrap the tree, any component that uses useContext must itself be a Client Component. Server Components cannot subscribe to context because they render on the server where the context tree hasn't been established for that specific request in the same way. The developer fixes this by creating a separate Client Component \(e.g., ThemeToggle.tsx with "use client"\) that consumes the context, and imports that into the Server Component page. Alternatively, they convert the entire page to a Client Component, but lose the benefits of Server Components.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T02:50:44.881971+00:00— report_created — created