useState only works in a Client Component
Last verified:
The fix
useState only works in a Client Component. This Next.js file is a Server Component until the first line is use client. Add that, or move the hook.
- 1
Move the interactive piece into its own file
Create a small component whose first line is
"use client". Keep the data fetch in the Server Component and pass plain props down."use client"; import { useState } from "react"; export function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>{count}</button>; } - 2
Do not mark the layout just to satisfy the error
Import the client component into the server page. Leave the layout as a Server Component unless the layout itself needs state.
- 3
Pass data, not functions, from the server
Props from a Server Component to a Client Component must be serializable. You can pass strings, numbers, and plain objects. A function prop has to be a server action (
"use server") or be defined inside the client file. - 4
Do not import a Server Component into a client file
A client file cannot import a server file. If a server component needs to appear inside a client component, pass it as
childrenfrom a server parent.
Why it happens
In the App Router, a component is a Server Component by default. Server Components can await data and read server env vars. They cannot use useState, useEffect, or onClick. Client Components can. The directive "use client" has to be the first statement in the file that uses them.
Putting that directive on layout.tsx or the page sends the whole subtree to the client, which is how a small button becomes a large client bundle. The directive also does not mean “browser only”. Client Components are still pre-rendered on the server, so window is not automatically safe, and a secret imported there ships to the browser. Importing fs, a database client, or another server-only module into the client file is the other boundary mistake. A key without NEXT_PUBLIC_ stays in the Server Component. Do not prefix it so a client child can see it.
The next error you'll probably hit
Once this is fixed, the usual follow-up is ReferenceError: window is not defined. window is not defined: Next.js read window, document, or localStorage on the server. Move that read into useEffect, or load the widget with ssr: false.
Prompt to paste back into your AI tool
Paste this above the error.
Next.js reports that useState (or another hook) only works in a Client Component. Do not add "use client" to the root layout or the page just to silence this. 1. Move the hook and the event handlers into a small file whose first line is "use client". 2. Keep data fetching in the Server Component and pass serializable props. 3. Do not import a Server Component into the client file. Use children if a server subtree must be nested. 4. Show the new file and the import change only.
Common questions
+Can I add "use client" at the top of page.tsx?
You can, and the error goes away, but the page and everything it imports become client code. Prefer a small client child so the page can still fetch on the server.
+I added "use client" and now window is not defined. Why?
The file still renders on the server once, for the initial HTML. Guard browser APIs inside useEffect, or load a browser-only widget with next/dynamic and ssr: false on that widget only.
+Does "use client" mean the component skips the server?
No. Client Components are still pre-rendered on the server for the first HTML. Browser-only APIs belong in an event handler or useEffect, not at the top of the file.
+Why not mark every file with "use client"?
Then nothing can read server-only data or a secret env var, and the browser downloads the whole tree. Keep data fetching on the server and mark the interactive piece.
Related errors
- ReferenceError: window is not definedwindow is not defined: Next.js read window, document, or localStorage on the server. Move that read into useEffect, or load the widget with ssr: false.Cursorv0BoltNext.jsReactOct 14, 2026
- Functions cannot be passed directly to Client ComponentsFunctions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". An onClick in a Server Component is the other sentence: Event handlers cannot be passed to Client Component props. Define UI handlers in the client file.Cursorv0BoltNext.jsReactOct 27, 2026
- Module not found: Can't resolve 'fs'Module not found: Can't resolve 'fs': client code imported fs and Next.js tried to bundle it. Move the import to a Server Component or route handler.Cursorv0BoltNext.jsNodeOct 16, 2026
- Dynamic server usage: Route couldn't be rendered statically because it used `cookies`Dynamic server usage: Route couldn't be rendered statically because it used `cookies`. Await cookies() on the same call stack, outside setTimeout, and do not swallow that throw. The route can then render per request.Cursorv0BoltNext.jsReactNov 6, 2026
- Hydration failed because the server rendered HTML didn't match the client.Hydration failed because the server rendered HTML didn't match the client. Render one tree first. Read the clock, a random value, or localStorage inside useEffect, after that first paint.CursorLovableBoltNext.jsReactNov 6, 2026
- Invalid hook call. Hooks can only be called inside of the body of a function component.React had no Hooks dispatcher when this function ran. The hook is outside a component, the Rules of Hooks were broken, or the page has two copies of React.CursorLovableBoltReactNext.jsOct 27, 2026