Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

useState fails in a child component in the Next.js App Router

ErrorYou're importing a component that needs useState. This React Hook only works in a Client Component.

Short answer

In the App Router every component is a Server Component unless a file above it is marked otherwise, and Server Components cannot use state or effects. Put 'use client' as the first line of the file that calls useState, keep it small, and wrap third-party components that lack the directive.

  • Applies to Next.js 15, React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The build error names the file that calls the hook. Open it and look at its first lines for the directive.

    head -n 5 app/ui/counter.tsx
  2. 2Match what you see, then jump to its fix

    1. 1The error points at your own component that imports useState or useEffectThe file that uses the hook has no 'use client'Fix
    2. 2You added 'use client' to a whole page and now server-only code (a database call, a secret) failsA page was marked as a client component when only a small part needs itFix
    3. 3The error trace ends inside node_modules when you render a package's component from a page or layoutA third-party component uses hooks but has no directiveFix

What does the error look like?

  • You're importing a component that needs useState. This React Hook only works in a Client Component.
  • React client hook in Server Component

Cause 1 of 3

The file that uses the hook has no 'use client'

1Check

Layouts and pages are Server Components by default. A component that uses state, event handlers or lifecycle effects must be a Client Component, so the directive has to be in that file.

grep -rLn "use client" $(grep -rl -E "useState|useEffect" app components 2>/dev/null)

2Fix

Add 'use client' at the very top of the file, above all imports.

'use client'

import { useState } from 'react'

export default function Counter() {
  const [count, setCount] = useState(0)
  return <button onClick={() => setCount(count + 1)}>{count}</button>
}

3Verify

Reload the route; the error is gone and the button counts.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

A page was marked as a client component when only a small part needs it

1Check

Everything a 'use client' file imports and renders directly goes into the client bundle. Server Components passed in as children are not pulled in.

grep -rn "use client" app | head -20

2Fix

Move the interactive part into its own small file with the directive, and import it from the page. Pass server data down as props.

// app/[id]/page.tsx (Server Component)
import LikeButton from '@/app/ui/like-button'
export default async function Page() {
  return <LikeButton likes={3} />
}

3Verify

The page still fetches on the server and the small component stays interactive.

npm run build

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

A third-party component uses hooks but has no directive

1Check

If the package's module does not declare 'use client', Next.js cannot know it needs client features, so using it from a Server Component errors.

grep -rl "use client" node_modules/acme-carousel 2>/dev/null | head -1

2Fix

Wrap it in a file of your own that has the directive, and import your wrapper instead.

// app/carousel.tsx
'use client'

import { Carousel } from 'acme-carousel'

export default Carousel

3Verify

Render the wrapper from a Server Component page.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Add 'use client' only to leaf components that need state, effects or browser APIs.
  • Use a children slot in a Client Component when it must wrap server-rendered content.
  • If you publish a component library, put the directive in its client entry points.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

React warns that a div is nested inside a p tag

Text content does not match server-rendered HTML

A p element may contain only phrasing content, and the HTML parser closes a paragraph as soon as a div, list or other block element starts.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.