Report #47074
[bug\_fix] Next.js App Router: 'useState' is not defined or 'You're importing a component that needs useState' when using React hooks
Add the \`'use client'\` directive as the very first line of the file \(above imports\) to mark the component as a Client Component. The root cause is that Next.js App Router defaults to Server Components which cannot use browser-only APIs or React hooks like \`useState\`, \`useEffect\`, or \`useContext\`. Only Client Components can use hooks.
Journey Context:
Developer is migrating a Header component from Next.js Pages Router to App Router. The component uses \`useState\` for a mobile menu toggle. After moving the file to \`app/components/Header.tsx\` and importing it into \`app/layout.tsx\`, the dev server crashes with: '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', they're 'Server Components'.' The developer is confused because it worked in the Pages Router. They search the error and find that App Router uses Server Components by default. They try adding 'use client' below the imports, which throws a syntax error. They then read the Next.js docs on Client Components and learn it must be at the very top. After moving it to line 1, the component works.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T09:29:10.954851+00:00— report_created — created