← All errors · Next.jsNext.js App Router & config errors

You're importing a component that needs useState. It only works in a Client Component

Exact error

You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.

What it means

In the App Router every component is a Server Component unless marked otherwise. Hooks like useState, useEffect and event handlers only work in Client Components.

Likely causes

How to fix it

  1. Add the directive as the very first line of the file that uses hooks:

    'use client';
    
    import { useState } from 'react';
    export default function Counter() {
      const [n, setN] = useState(0);
      return <button onClick={() => setN(n + 1)}>{n}</button>;
    }
  2. Keep "use client" as low in the tree as possible: extract the interactive part into its own small component and import it from the Server Component page.

Prevent this next time

FAQ

What does "You're importing a component that needs useState. It only works in a Client Component" mean?

In the App Router every component is a Server Component unless marked otherwise. Hooks like useState, useEffect and event handlers only work in Client Components.

What usually causes it?

Using React hooks in a file under app/ without the "use client" directive. Passing onClick or other handlers from a Server Component.

How do I fix it?

1. Add the directive as the very first line of the file that uses hooks: 2. Keep "use client" as low in the tree as possible: extract the interactive part into its own small component and import it from the Server Component page.

Related errors

Get new fixes weekly

No spam. Unsubscribe anytime. Privacy