---
title: "You're importing a component that needs useState. It only works in a Client Component"
description: "Why the Next.js App Router refuses hooks like useState in Server Components, and how to add the use client directive in the right place without making your whole app client-side."
url: https://www.danny.engineering/answer/nextjs/youre-importing-a-component-that-needs-usestate-it-only-works-in-a-client-component
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Next.js"]
---

# You're importing a component that needs useState. It only works in a Client Component

## The short answer


In the App Router, files are Server Components by default, and Server Components cannot use state, effects or browser events. Add the `'use client'` directive to the top of the file that uses the hook, and keep that file as small as you can.


```typescript
'use client';

import { useState } from 'react';

export function LikeButton() {
  const [likes, setLikes] = useState(0);
  return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button>;
}
```


## Why it happens


Server Components run once on the server and send their output to the browser. They never re-render in response to user input, so hooks that hold state or react to the browser have nothing to do there. That covers `useState`, `useReducer`, `useEffect`, `useLayoutEffect`, `useRef` used for DOM work, `useContext` for client context, and event handlers such as `onClick`.


The `'use client'` directive marks a boundary. The file it sits in, and everything that file imports, becomes part of the client bundle. Without it, Next.js treats the module as a Server Component and stops the build when it sees a client-only hook.


You will often hit this when you install a package that uses hooks internally but does not ship its own `'use client'` directive, or when you add a click handler to a page component.


## How to fix it


### Move the interactive part into its own Client Component


The recommended approach is to keep your page as a Server Component and extract only the interactive piece. The page can still fetch data on the server and pass it down as props.


```typescript
// app/posts/[id]/page.tsx (Server Component)
import { LikeButton } from './like-button';

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const post = await getPost(id);

  return (
    <article>
      <h1>{post.title}</h1>
      <LikeButton />
    </article>
  );
}
```


`like-button.tsx` is the file from the short answer, with `'use client'` at the top. Only that small component ships JavaScript to the browser.


### Wrap third-party components


If a library component uses hooks but has no directive, create a thin wrapper and import the wrapper instead.


```typescript
// components/carousel.tsx
'use client';

export { Carousel } from 'some-carousel-library';
```


### Pass Server Components as children


A Client Component cannot import a Server Component, but it can render one passed in as `children` or another prop. This is how you put a client-side provider around server-rendered content.


```typescript
// app/providers.tsx
'use client';

import { ThemeProvider } from './theme';

export function Providers({ children }: { children: React.ReactNode }) {
  return <ThemeProvider>{children}</ThemeProvider>;
}
```


Use `Providers` in `app/layout.tsx` and the pages inside it stay as Server Components.


## Common pitfalls

- Adding `'use client'` to `layout.tsx` or a top-level page. It works, but it turns everything below it into client code and you lose server-side data fetching and smaller bundles.
- Placing the directive below the imports. It must be the first statement in the file, before any imports. Comments above it are fine.
- Expecting props to accept anything. Props passed from a Server Component to a Client Component must be serialisable, so plain objects, arrays, strings and numbers are fine, but class instances and ordinary functions are not. Server Actions are the exception.
- Importing server-only code, such as a database client, into a file marked `'use client'`. Use the `server-only` package to make that a build error.

## Related reading

- [Next.js: Server and Client Components](https://nextjs.org/docs/app/getting-started/server-and-client-components)
- [React: use client](https://react.dev/reference/rsc/use-client)
