More Next.js answers

by Danny
Published 2 min read

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.

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.


_10
'use client';
_10
_10
import { useState } from 'react';
_10
_10
export function LikeButton() {
_10
const [likes, setLikes] = useState(0);
_10
return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button>;
_10
}

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.


_14
// app/posts/[id]/page.tsx (Server Component)
_14
import { LikeButton } from './like-button';
_14
_14
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
_14
const { id } = await params;
_14
const post = await getPost(id);
_14
_14
return (
_14
<article>
_14
<h1>{post.title}</h1>
_14
<LikeButton />
_14
</article>
_14
);
_14
}

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.


_10
// components/carousel.tsx
_10
'use client';
_10
_10
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.


_10
// app/providers.tsx
_10
'use client';
_10
_10
import { ThemeProvider } from './theme';
_10
_10
export function Providers({ children }: { children: React.ReactNode }) {
_10
return <ThemeProvider>{children}</ThemeProvider>;
_10
}

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

Was this resource helpful?