A useState component without "use client" works inside a Client Component. What happens when a Server Component page renders it too?
CupCounter keeps a cup count in useState, and its file has no "use client". For weeks it has only been rendered by OrderBox, whose file starts with "use client", and the shop built and ran fine. Today the tea page, a Server Component, renders CupCounter a second time, for a gift box. What does next build say?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/cup-counter.tsximport { useState } from "react";
export default function CupCounter() {
const [cups, setCups] = useState(1);
return (
<p>
Cups: {cups} <button onClick={() => setCups(cups + 1)}>Add a cup</button>
</p>
);
}
app/order-box.tsx"use client";
import CupCounter from "./cup-counter";
export default function OrderBox() {
return (
<section>
<h2>Your order</h2>
<CupCounter />
</section>
);
}
app/page.tsximport CupCounter from "./cup-counter";
import OrderBox from "./order-box";
export default function TeaPage() {
return (
<main>
<h1>Mint tea</h1>
<OrderBox />
<h2>Add to a gift box</h2>
<CupCounter />
</main>
);
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build. What happens:
- You run
next build.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…