You're importing a component that needs "use client"

Filed under Next.js & React errors

Most likely causeAbout 10 min · 6 steps

A file rendered on the server is using browser-only features like useState or onClick without "use client" at the top.

Open the file the error names
Add "use client" as the first line
Or split the interactive part into its own file

Fastest route

Give your AI this prompt

Copy it, replace the bracketed part with your error, and send it.

Preview the prompt
I am getting this Next.js App Router error: "You're importing a component that needs use client" — the full message says a React hook only works in a client component.

Here is the full error and the file it points to: [paste error and the component file].

Please: 1) tell me which line in that file crosses the server/client boundary — a hook, an event handler, or a browser-only library, 2) fix it the small way first: add "use client" as the very first line of that file if the whole file is interactive, or move the interactive part into its own small client component file and import it, keeping the rest of the page a Server Component, 3) if the error mentions event handlers passed as props, move the handler into the client file instead of passing a function down.

Constraints: do not add "use client" to my root layout or to unrelated pages, do not convert data-fetching server code into client code, make one change at a time, and tell me which cause you found before you change code.

Fix it yourself

Follow the repair

  1. Find the file the error names. Read the component name and file path in the error output instead of guessing. The file to fix is the one that calls the hook or defines the handler — usually the file named right in the message, not the page that happens to import it.
    // The error prints the culprit, something like:
    // You're importing a component that needs "use client".
    // ... at LikeButton (app/like-button.tsx:6:21)
  2. Add "use client" as the very first line. Put the directive on line one of that file, above every import, spelled exactly like this with the quotes. Position is not a detail: after the imports, or inside the component, it is just a stray string of text and the error will stay exactly as it is.
    "use client";
    
    import { useState } from "react";
  3. Restart the dev server. Next.js reads directives when the project starts, so a running dev server can keep showing the old error after you have already fixed the file. Stop it and start it again. If the error survives a restart, the directive is almost certainly in a different file than the one the error named — go back to step 1.
  4. Better: split the interactive piece into its own file. The one-line fix works, but the pattern Next.js wants is small client islands inside server pages. Keep the page a Server Component, move just the button, counter, or form into its own file that starts with the directive, and import it. The page keeps its fast server rendering and data fetching, and only the little island ships extra JavaScript to the browser.
    // app/like-button.tsx — the interactive island
    "use client";
    import { useState } from "react";
    
    export default function LikeButton() {
      const [likes, setLikes] = useState(0);
      return <button onClick={() => setLikes(likes + 1)}>{likes} likes</button>;
    }
    
    // app/page.tsx — stays a Server Component, just imports it
    import LikeButton from "./like-button";
  5. Do not paste the directive into your layout to make it go away. It is tempting when the error keeps pointing at new files, but adding "use client" to a root layout or a whole page turns everything underneath it into client code. Visitors download more JavaScript, and you get fresh errors if that file also fetches data on the server or exports metadata, which client files cannot do. Small files, marked one at a time, is the fix that stays fixed.
  6. If the error is about props, keep functions on the client side. A close cousin of this error reads "Event handlers cannot be passed to Client Component props." It means a server file tried to hand a function — usually an onClick — down into a client component, and functions cannot cross that boundary. Move the handler into the client file itself, or pass plain data across — text, numbers, IDs — and let the client component decide what to do with it.
Why this happensOptional explanation

In the Next.js App Router, components render on the server by default. That is a feature: the server can fetch data, keep secrets safe, and send finished HTML quickly. The trade-off is that server-rendered code runs once, in an environment with no browser — so it cannot remember anything (no useState), cannot react to anything (no onClick), and cannot use effects or browser APIs.

"use client" is a boundary marker you put at the top of a file to say: this part, and everything it imports, also belongs in the browser. When Next.js finds a hook or a handler in a file without that marker, it stops with this error rather than shipping a page whose buttons silently do nothing. The message feels abrupt, but it is Next.js pointing at the exact file that crossed the line.

  • A hook in a server file: useState, useEffect, useRef, or a custom hook called in a component with no directive at the top.
  • An event handler in a server file: onClick, onChange, or onSubmit written in a page or component that renders on the server.
  • The directive in the wrong place: typed after the imports, in a different file than the one using the hook, or missing its quotes — all are treated as no directive at all.
  • A browser-only library imported on the server: some chart, carousel, and editor packages need the browser; importing one into a server file raises the same error.

Quick answers

Does adding "use client" stop my page from rendering on the server?

No. Client Components still get pre-rendered to HTML on the server, so visitors see the page just as fast. The directive only marks where the browser-side boundary is, so React knows this part can hold state and respond to clicks.

It worked yesterday. Why did adding one button break it?

Because a button with an onClick is interactive, and interactivity is exactly what Server Components cannot do. The moment a file uses a hook or an event handler, that file has to become a Client Component — the code did not get worse, it crossed the boundary.

Can a Server Component import a Client Component?

Yes — that is the pattern Next.js is designed around. A server page can import a small client island. What you cannot do is the reverse: a client file cannot import a server-only file, though it can receive one as its children.

One fix, every Friday.

One real error, explained in plain English, with the fix — in your inbox every Friday. No spam, no jargon, unsubscribe anytime.