Report #12548
[bug\_fix] window is not defined or useState is not a function in Server Component
Add the 'use client' directive at the very top of the file \(above imports\) to mark the file as a Client Component, allowing the use of browser APIs and React hooks.
Journey Context:
Developer creates a new file \`components/Counter.tsx\` that uses \`useState\` to manage a count. They import this into \`app/page.tsx\` \(a Server Component by default in Next.js App Router\). Immediately, the dev server throws an error: 'Error: useState is not a function' or during build 'window is not defined'. The developer checks if React is installed correctly, checks for duplicate React versions, and tries importing React explicitly. They realize the component works fine in Storybook \(which is client-only\). After searching, they discover that in the Next.js App Router, all components are Server Components by default and cannot use hooks or browser APIs. The fix is simply adding \`'use client'\` on the first line of the file, which signals to Next.js to bundle this component for the browser instead of rendering it on the server.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:17:37.992971+00:00— report_created — created