Report #98688
[bug\_fix] Next.js App Router build/dev error: 'You're importing a component that needs useState. That only works in a Client Component...' or 'window is not defined' when using a hook or browser API in a page or component.
Add the 'use client' directive at the very top of the file \(above imports\) for any component that uses client-only features: hooks, event handlers, browser APIs, context providers, or third-party libraries that use them. Keep parent layouts and pages as Server Components and pass Server Components as children or props into the Client Component to avoid shipping unnecessary JavaScript.
Journey Context:
Created app/contact/page.tsx and dropped in a form with useState and an onSubmit handler. next dev immediately threw 'You're importing a component that needs useState. That only works in a Client Component but none of its parents are marked with "use client"'. I was confused because the same code worked in the Pages Router. Reading the Next.js client-components docs clarified that App Router files default to Server Components and cannot use hooks or browser APIs. I extracted the interactive form into app/contact/form.tsx, added 'use client' at the top, and kept page.tsx as a Server Component that passes static content as children. The bundle size stayed small and the error went away.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:36:33.858002+00:00— report_created — created