Next.js: React client hook in a Server Component
In the App Router, components are Server Components by default and can't use client hooks like useState or useEffect. Add 'use client' at the top of the file that uses the hook, or move the interactive part into a small client component.
Cause: A hook that needs the browser runs in a component without the 'use client' directive.
How to fix it
- Find the component using the hook (the error points to it).
- Add
'use client'as the first line of that file. - Keep data fetching in Server Components; pass data to the client component as props.
- Don't mark whole layouts as client unless needed.
Example
Illustrative example, not from a customer project.
+ 'use client'
import { useEffect } from 'react'
FAQ
Does 'use client' hurt SEO?
Client Components are still prerendered to HTML; they just also hydrate in the browser.
Why did this appear after migrating to the App Router?
The Pages Router had no Server Components; the App Router defaults to them.
Get a $149 diagnosis See pricing Launching soon? The Launch Path gets you ready for $1,195 flat.
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- read-only access; fixes arrive as pull requests you merge
- Your $149 diagnosis is credited in full toward any paid job.