Performance: Why conditional rendering is needed
MEDIUM IMPACT
Conditional rendering affects how much content the browser needs to process and display, impacting page load speed and interaction responsiveness.
function Component({ showHeavy }) { return ( <div> {showHeavy ? <HeavyComponent /> : null} <LightComponent /> </div> ); }
function Component() { return ( <div> <HeavyComponent /> <LightComponent /> </div> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Render all components unconditionally | High (all nodes created) | Multiple reflows | High paint cost | [X] Bad |
| Render components conditionally | Low (only needed nodes) | Single or fewer reflows | Lower paint cost | [OK] Good |