Next.jsApp RouterError Fix2026 Updated

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

January 20266 min readNext.js 14/15 · App Router · React 19

⚡ The Error

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

any-component.tsx
"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.

1

Add 'use client' to the file

Root cause fix — always try this first
1 min

Place "use client" as the absolute first line of any file that uses hooks or browser APIs. It must come before all import statements.

components/counter.tsx
"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.

2

Extract an interactive child component

Best pattern — keep Server Components as data fetchers
5 min

Rather 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.

app/products/page.tsx — Server 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>
  )
}
components/add-to-cart-button.tsx — Client Component
"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>
  )
}
3

Move hooks out of Server Components

Common mistake when migrating from Pages Router
3 min

When 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.

Before — broken in App Router
// 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>
}
After — correct App Router pattern
// 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>
}
4

Fix third-party library components

react-icons, framer-motion, chart.js wrappers
3 min

Many 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.

components/chart-wrapper.tsx
"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>
  )
}
app/reports/page.tsx — still a Server Component
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>
  )
}
5

Understand the client boundary tree

Architecture — avoid over-using 'use client'
4 min

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 boundary placement
// ✅ 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 Component

Avoid 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

Frequently Asked Questions

What does 'use client' mean in Next.js?+
'use client' is a directive you place at the top of a file to mark it and its imports as a Client Component. Without it, every component in the App Router is a Server Component by default, which cannot use React hooks like useState, useEffect, or browser APIs.
Where exactly do I put 'use client'?+
Place 'use client' as the very first line of the file, before any imports. It must be a string literal at the top of the module — not inside a function, not after an import statement.
Do I need 'use client' on every file that uses hooks?+
No. 'use client' marks a boundary — every component imported into a 'use client' file automatically becomes a Client Component too. You only need the directive on the topmost file in a subtree that requires client-side behaviour.
Can a Server Component import a Client Component?+
Yes. Server Components can import and render Client Components. The reverse is not true — Client Components cannot import Server Components (but they can receive them as children props).
Why does my third-party library cause a 'use client' error?+
Many npm packages use hooks or browser APIs internally but don't include the 'use client' directive because they were written before Next.js App Router existed. Wrap them in your own client component file that has 'use client' at the top.
Can I use useState in a Server Component?+
No. useState, useEffect, useRef, useCallback, useMemo, and all other hooks are client-only. They require a browser environment and the React reconciler, which is only available on the client side in App Router.

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