Performance: If conditions in JSX
MEDIUM IMPACT
This affects rendering speed and interaction responsiveness by controlling how many elements React creates and updates in the DOM.
function Component({ show }) { if (!show) return <AnotherExpensiveComponent />; return <ExpensiveComponent />; }
function Component({ show }) { return ( <div> {show ? <ExpensiveComponent /> : <AnotherExpensiveComponent />} {show && <ExpensiveComponent />} </div> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple conditional components rendered simultaneously | High (many nodes created) | Multiple reflows | High paint cost | [X] Bad |
| Single conditional return with if statement | Low (only needed nodes) | Single reflow | Low paint cost | [OK] Good |