Skip to content
>_VibeUnstuck

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. 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. 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. 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. 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 children from 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