Report #95613
[bug\_fix] Next.js App Router Server Component cannot use React hooks or browser APIs \(missing 'use client'\)
Add the \`'use client'\` directive at the very top of the file \(before imports\) to mark the file as a Client Component, allowing the use of useState, useEffect, and browser APIs.
Journey Context:
Developer creates a new file \`components/Counter.tsx\` with \`import \{ useState \} from 'react'\` and uses the hook. They import this into a page.tsx in the App Router. The build fails with an error like '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"'. The developer tries adding 'use client' to the parent page.tsx, but ideally wants to keep the page as a Server Component. Realizing that any file using hooks must be a Client Component, they add the string \`'use client'\` at the very top of \`Counter.tsx\` \(above the imports\). The error clears and the component works. They note that this reduces the portion of the UI that can be server-rendered, so they consider if the state could be lifted to a parent Client Component instead.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:04:13.810510+00:00— report_created — created