Report #49342
[bug\_fix] Error: Cannot read properties of undefined \(reading 'map'\) or Context value is undefined when using useContext
Ensure the Context Provider wraps the component tree at a higher level than where useContext is called. If using Next.js App Router, ensure the Provider is in a Client Component \('use client'\) and imported into the root layout or specific layout.
Journey Context:
Developer creates a React Context in a separate file \(ThemeContext.tsx\) with createContext and a Provider component. They try to consume it in a deeply nested component using useContext\(ThemeContext\) and get undefined, causing destructuring errors. They check if they wrapped the app with the Provider and realize they only exported it but never used it in \_app.tsx \(Pages Router\) or layout.tsx \(App Router\). In App Router, they add it to the root layout but get an error that Context requires client features. They then have to make the Provider a Client Component by adding 'use client' to the file, then import it into the Server Component layout. The context is now available throughout the tree. Why it works: React Context uses the component tree ancestry to propagate values. If the Provider isn't an ancestor, useContext returns the default value \(undefined if not set\). In Next.js App Router, context requires the 'use client' directive because it relies on client-side React tree state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T13:18:19.425197+00:00— report_created — created