← All errors · Next.js

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.

Related errors

Get new fixes weekly

No spam. Unsubscribe anytime. Privacy