Performance: Client component boundaries
This affects how much JavaScript is sent to the browser and how fast the page becomes interactive.
import React from 'react'; import Counter from './Counter'; export default function Page() { return ( <div> <h1>Welcome</h1> <Counter /> <p>Static content here</p> </div> ); } // Counter.tsx 'use client'; import React from 'react'; function Counter() { const [count, setCount] = React.useState(0); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; } export default Counter;
'use client'; import React from 'react'; export default function Page() { // Entire page is a client component const [count, setCount] = React.useState(0); return ( <div> <h1>Welcome</h1> <button onClick={() => setCount(count + 1)}>Count: {count}</button> <p>Static content here</p> </div> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Whole page as client component | High (all nodes hydrated) | Multiple reflows during hydration | High paint cost due to JS execution | [X] Bad |
| Small isolated client components | Low (only interactive parts hydrated) | Single reflow per component | Lower paint cost, faster interaction | [OK] Good |
"use client"; in Next.js?"use client"; at the top of a file tells Next.js this component runs in the browser, enabling interactivity."use client"; at the very top of the file to mark it as a client component."use client";
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}import { useState } from 'react';
export default function Clicker() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}Profile that fetches user data and a client component EditButton that allows editing the profile. How should you correctly use these components together?EditButton must have "use client" at the top to run in the browser, while Profile remains a server component.EditButton inside Profile and mark EditButton with "use client"; this keeps boundaries correct and enables interactivity.Pass the test and this pattern moves unknown