Performance: Why state management differs in Next.js
MEDIUM IMPACT
This affects how quickly the page can load and update by managing where and when state changes happen in Next.js apps.
'use client'; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>; } // Embed in Server Component: async function Page() { return <Counter />; } export default Page; // Note: Pure UI state like counters must be client-side, but isolate in small client components to minimize re-renders impact.
import { useState } from 'react'; export default function Page() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side state only | Many updates on client | Multiple reflows per update | High paint cost on interaction | [X] Bad |
| Server-side state with server components | Minimal client updates | Single or no reflows on interaction | Low paint cost | [OK] Good |
| Client data fetching in useEffect | Delayed DOM update | Reflow on data arrival | Paint triggered after load | [!] OK |
| Server data fetching in server component | DOM ready with data | No reflows on load | Paint happens once | [OK] Good |