Performance: Mounting phase
MEDIUM IMPACT
This affects the initial page load speed and how quickly React components appear on screen.
import { useState, useEffect } from 'react'; function MyComponent() { const [data, setData] = useState(null); useEffect(() => { setTimeout(() => { const result = heavyComputation(); setData(result); }, 0); }, []); if (data === null) return <div>Loading...</div>; return <div>{data}</div>; } function heavyComputation() { let sum = 0; for (let i = 0; i < 1e7; i++) sum += i; return sum; }
function MyComponent() { const data = heavyComputation(); return <div>{data}</div>; } function heavyComputation() { let sum = 0; for (let i = 0; i < 1e7; i++) sum += i; return sum; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous logic in render | 1 (initial mount) | 1 (initial layout) | High (delayed paint) | [X] Bad |
| Deferred heavy logic with useEffect | 1 (initial mount) | 1 (initial layout) | Low (fast paint) | [OK] Good |