Report #47319
[bug\_fix] You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with 'use client'
Add the \`'use client'\` directive at the absolute top of the file \(before any imports\) where you use browser APIs \(\`window\`, \`document\`, \`localStorage\`\), React hooks \(\`useState\`, \`useEffect\`\), or third-party libraries that access the DOM. Root cause: Next.js App Router uses React Server Components by default, which execute in a Node.js environment without browser globals; the explicit directive opts the module into the client bundle.
Journey Context:
You just migrated your blog from Next.js Pages Router to the App Router. You move your \`pages/index.tsx\` to \`app/page.tsx\` and copy over your \`HeroSection\` component that uses \`useState\` to manage a mobile menu toggle. When you run \`npm run dev\`, the screen shows a red error overlay: "You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with 'use client', add the 'use client' directive at the top of the file." You try adding \`'use client'\` to \`page.tsx\`, which fixes it, but then you realize the entire page is now a Client Component and you lose the benefits of Server Components. You refactor: you move the interactive part into a new file \`MobileNav.tsx\`, add \`'use client'\` only to that file, and import it into the Server Component \`page.tsx\`. The error disappears and the page can still fetch data on the server. You learn that \`'use client'\` creates a boundary; everything imported below it is treated as client code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T09:54:38.470737+00:00— report_created — created