useState fails in a child component in the Next.js App Router
You'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.
Fix it fast
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.tsx2Match what you see, then jump to its fix
- 1
The error points at your own component that imports useState or useEffectThe file that uses the hook has no 'use client'Fix - 2
You 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
The 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
- 1
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 devConfirmed by nextjs.org
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 -202Fix
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 buildConfirmed by nextjs.org
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 -12Fix
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 Carousel3Verify
Render the wrapper from a Server Component page.
npm run devConfirmed by nextjs.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixWeb apps
Warning: Extra attributes from the server in the console
Text content does not match server-rendered HTML
React found attributes on an element in the server HTML that its own render did not produce.
3 causes, each with a fixWeb apps
Next.js stops with 'document is not defined' while it renders on the server
ReferenceError: document is not defined
Next.js renders your components on the server first, where there is no document.
3 causes, each with a fixWeb apps
A custom error from the server arrives at the client as a generic message
error.digest holds a hash instead of your message
Next.js hides the message of errors thrown in Server Components in production, to avoid leaking details.
3 causes, each with a fixWeb apps
React warns about updating one component while another renders
To locate the bad setState() call inside `Child`, follow the stack trace
React renders must be pure.
4 causes, each with a fixWeb 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