Skip to content
jshacks.dev
Frameworks

Keep server components server-only by default

In the Next.js 16 App Router, leave components as Server Components and add use client only to the small interactive leaf that needs state or events.

1 min read
Summarize in AI

Every component in the App Router is a Server Component until a file says otherwise. Adding 'use client' at the top of a big file sends all of it, and everything it imports, to the browser.

app/products/page.jsJSX
import { LikeButton } from './LikeButton';

async function getProduct() {
  const res = await fetch('https://api.example.com/products/1');
  return res.json();
}

export default async function ProductPage() {
  const product = await getProduct();

  return (
    <article>
      <h1>{product.name}</h1>
      <p>{product.description}</p>
      <LikeButton />
    </article>
  );
}
app/products/LikeButton.jsJSX
'use client';

import { useState } from 'react';

export function LikeButton() {
  const [liked, setLiked] = useState(false);
  return <button onClick={() => setLiked(!liked)}>{liked ? '♥' : '♡'}</button>;
}
Text
Server: renders <h1>, <p> and the data fetch      (no JavaScript sent for them)
Client: hydrates only LikeButton and its useState

The page fetches data and renders static markup on the server. Only LikeButton, which needs useState and onClick, is marked as a Client Component. The directive marks a boundary, so everything imported into that file joins the client bundle. See Server and Client Components.

Push 'use client' down to the smallest leaf. If a file only needs a hook for a few lines, extract those lines into their own component rather than marking the whole page.

Taggednextjs 16
Share

More snippets

Frameworks

Update state from the previous value

Pass an updater function to the React state setter when the next value depends on the last one, so batched updates do not overwrite each other.

1 min read
Frameworks

Manage state with useState

Keep a changing value in a React 19 component with useState, and see why the setter schedules a re-render instead of changing the variable.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.