A practical mental model for React Server Components
Most confusion about Server Components comes from treating them as a new kind of server-side rendering. They are better understood as a way to split one component tree across two environments, with the split decided by you.
The rule of thumb
- Server Components fetch data and render markup. They never ship their code to the browser.
- Client Components handle state, effects and event handlers. Mark them with
"use client". - Everything a Client Component imports becomes client code too, so keep the boundary low in the tree.
Push interactivity to the leaves
A product page does not need to be a Client Component because it has one Add to cart button. Keep the page on the server, and make only the button a client island.
// app/product/[id]/page.tsx (Server Component)
export default async function Page({ params }: PageProps<"/product/[id]">) {
const { id } = await params;
const product = await getProduct(id);
return (
<article>
<h1>{product.name}</h1>
<p>{product.description}</p>
<AddToCart id={product.id} /> {/* the only client code */}
</article>
);
}Pass server output into client components
Client Components can receive Server Components as children. That lets an interactive wrapper, like a tab panel or a modal, hold content that was rendered on the server and never hydrated.
What this buys you
Smaller bundles, data fetching next to the component that needs it, and secrets that cannot leak to the browser. The cost is that you have to think about where the boundary sits. Once you do it on purpose, it becomes the most useful architectural decision in the app.
Next post