Next.js Error: You're importing a component that needs useState— 'use client' Directive Fix [2026]
Also covers: hooks in Server Components · client boundary · third-party libraries · App Router migration
⚡ The 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.✅ Fastest Fix
"use client" // ← add this as the very first line
import { useState } from "react"
export default function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(c => c + 1)}>{count}</button>
}What Causes the 'use client' Error?
In Next.js App Router, every component is a Server Component by default. Server Components run only on the server — they have no access to browser APIs, event handlers, or React hooks like useState and useEffect. When you use a hook in a Server Component (or import a library that does), Next.js throws this error because it detects a client-only API in a server-only context.
The fix is always to mark the component (or its boundary ancestor) with the "use client" directive — turning it into a Client Component that runs in the browser.
Add 'use client' to the file
Root cause fix — always try this firstPlace "use client" as the absolute first line of any file that uses hooks or browser APIs. It must come before all import statements.
"use client"
import { useState, useEffect } from "react"
export default function Counter() {
const [count, setCount] = useState(0)
useEffect(() => {
document.title = `Count: ${count}`
}, [count])
return (
<button onClick={() => setCount(c => c + 1)}>
Clicked {count} times
</button>
)
}'use client' marks a boundary — every component imported into this file also becomes a Client Component automatically. You do not need to add 'use client' to every file in the subtree.
Extract an interactive child component
Best pattern — keep Server Components as data fetchersRather than converting your entire page to a Client Component, extract only the interactive parts into a small client component. This keeps the page as a Server Component (which can fetch data, access the database, etc.) and delegates client logic to a leaf component.
// No "use client" here — this stays a Server Component
import { AddToCartButton } from "@/components/add-to-cart-button"
async function getProduct(id: string) {
// Can fetch from DB directly — server only
const product = await db.product.findUnique({ where: { id } })
return product
}
export default async function ProductPage({ params }: { params: { id: string } }) {
const product = await getProduct(params.id)
return (
<div>
<h1>{product.name}</h1>
<p>{product.description}</p>
{/* Client Component rendered inside Server Component — totally fine */}
<AddToCartButton productId={product.id} price={product.price} />
</div>
)
}"use client"
import { useState } from "react"
export function AddToCartButton({ productId, price }: { productId: string; price: number }) {
const [added, setAdded] = useState(false)
return (
<button
onClick={async () => {
await fetch("/api/cart", { method: "POST", body: JSON.stringify({ productId }) })
setAdded(true)
}}
>
{added ? "Added!" : `Add to cart — $${price}`}
</button>
)
}Move hooks out of Server Components
Common mistake when migrating from Pages RouterWhen migrating from the Pages Router, it is easy to move a component file to the App Router without realising it became a Server Component. Identify every hook usage and move it to a dedicated client file.
// app/dashboard/page.tsx
// Missing "use client" — will throw the error
import { useState, useEffect } from "react"
export default function DashboardPage() {
const [data, setData] = useState(null) // ❌ hook in Server Component
useEffect(() => { // ❌ hook in Server Component
fetch("/api/stats").then(r => r.json()).then(setData)
}, [])
return <div>{data?.visits}</div>
}// app/dashboard/page.tsx — Server Component fetches data directly
export default async function DashboardPage() {
const data = await fetch("https://api.example.com/stats").then(r => r.json())
return <DashboardClient initialData={data} />
}
// app/dashboard/dashboard-client.tsx — Client Component for interactivity
"use client"
import { useState } from "react"
export function DashboardClient({ initialData }: { initialData: any }) {
const [data, setData] = useState(initialData)
return <div>{data?.visits}</div>
}Fix third-party library components
react-icons, framer-motion, chart.js wrappersMany popular libraries (charting, animation, UI) use hooks internally but predate the App Router. Wrap them in a thin client component to create the boundary Next.js needs.
"use client"
// framer-motion, recharts, react-chartjs-2 etc. all need this wrapper
import { LineChart, Line, XAxis, YAxis, Tooltip } from "recharts"
export function RevenueChart({ data }: { data: { month: string; revenue: number }[] }) {
return (
<LineChart width={600} height={300} data={data}>
<XAxis dataKey="month" />
<YAxis />
<Tooltip />
<Line type="monotone" dataKey="revenue" stroke="#6366f1" />
</LineChart>
)
}import { RevenueChart } from "@/components/chart-wrapper"
export default async function ReportsPage() {
const data = await getRevenueData() // server-side DB call
return (
<div>
<h1>Revenue Report</h1>
<RevenueChart data={data} />
</div>
)
}Understand the client boundary tree
Architecture — avoid over-using 'use client'A common mistake is adding "use client" to too many files, effectively turning your entire app into a client-rendered SPA and losing all Server Component benefits. The boundary should be as low as possible in the component tree.
// ✅ Good — "use client" only on the interactive leaf
app/page.tsx → Server Component (fetches data)
└── ProductList.tsx → Server Component (renders list)
└── AddToCart.tsx → "use client" (only interactive part)
// ❌ Bad — unnecessary "use client" bubbles up
"use client"
app/page.tsx → Client Component (loses SSR)
└── ProductList.tsx → Client Component (loses SSR)
└── AddToCart.tsx → Client ComponentAvoid adding 'use client' to layout.tsx or the root page unless absolutely necessary. This converts your entire route to client rendering, removing all performance and SEO benefits of the App Router.
Prevention
- Default to Server Components — only add 'use client' when you actually need hooks or browser APIs
- Place 'use client' as the first line, before all import statements
- Extract interactive parts to small leaf components rather than converting entire pages
- Wrap third-party libraries that use hooks in your own 'use client' wrapper file
- Never add 'use client' to layout.tsx unless every route in that layout needs client rendering
- Use the Next.js ESLint plugin — it flags missing 'use client' directives automatically
Frequently Asked Questions
What does 'use client' mean in Next.js?+−
Where exactly do I put 'use client'?+−
Do I need 'use client' on every file that uses hooks?+−
Can a Server Component import a Client Component?+−
Why does my third-party library cause a 'use client' error?+−
Can I use useState in a Server Component?+−
Need Expert Help?
We Build Next.js App Router Applications
Softplix engineers build production Next.js apps with correct Server/Client Component architecture. Struggling with App Router migration or performance? Let us help.
Talk to an Engineer