Report #29820
[bug\_fix] useContext returns default value instead of provided value or undefined in Next.js App Router
Ensure the component consuming context is rendered inside the Provider tree. Check for multiple instances of React or the context module \(common in monorepos with symlinks or incorrect bundling\). In Next.js App Router, ensure the Provider is marked with 'use client' if it manages client-side state, and is properly imported. If the Provider is a Server Component, ensure it's not accidentally creating a new context instance per request.
Journey Context:
Developer creates AuthProvider in App Router, wraps layout.tsx. Child component uses useContext\(AuthContext\) but gets undefined. Checks tree in DevTools, sees Provider is there. Tries moving Provider to template.tsx, same issue. Realizes they created the context in a file that is both imported by server and client, but the Provider is in a Server Component while the consumer is in a Client Component, potentially creating module instances. Or realizes they forgot to mark the Provider file with 'use client' because it uses useState internally. Marks Provider with 'use client', ensures single module instance, context works.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:26:40.663966+00:00— report_created — created