Performance: When to use client components
This affects page load speed and interaction responsiveness by deciding which parts of the page run in the browser versus on the server.
"use client"; import React from 'react'; export default function Button() { const [count, setCount] = React.useState(0); return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>; }
export default function Button() { const [count, setCount] = React.useState(0); return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>; } // No 'use client' directive, so this runs as a server component
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using client components only for interactive parts | Minimal, scoped to interactive elements | Single reflow on interaction | Low paint cost | [OK] Good |
| Marking entire page as client component unnecessarily | Large DOM with many nodes | Multiple reflows during hydration | High paint cost | [X] Bad |
"use client"; at the top of the file to mark it as a client component.useState in a Next.js server component without marking it as a client component?useState only work in client components because they run in the browser.useState in a server component without "use client" directive, Next.js throws a runtime error."use client"; but forgot to import useState from React. What will happen?useState must be explicitly imported from React to be used.useState is undefined.Pass the test and this pattern moves unknown