Performance: Server state vs client state
HIGH IMPACT
This concept affects page load speed and interaction responsiveness by determining where data is fetched and stored.
export async function getServerSideProps() { const res = await fetch('https://api.example.com/data'); const data = await res.json(); return { props: { data } }; } export default function Page({ data }) { return <div>{data.content}</div>; }
export default function Page() { const [data, setData] = React.useState(null); React.useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); if (!data) return <p>Loading...</p>; return <div>{data.content}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client fetch on mount | Minimal initial DOM | Multiple reflows due to loading state changes | Multiple paints for loading and content | [X] Bad |
| Server-side data fetch | Full DOM with data on load | Single reflow on initial render | Single paint with content | [OK] Good |
| Client state for UI interactions | DOM updates on interaction | Reflows only on state change | Paints only on changed elements | [OK] Good |
| Server state for UI interactions | Network requests on each interaction | Reflows delayed by network | Paints delayed by data arrival | [X] Bad |