Performance: Why advanced patterns solve complex UIs
HIGH IMPACT
This concept impacts page load speed and interaction responsiveness by managing how complex UI updates are handled efficiently.
import { useState, useCallback, memo } from 'react'; const Part = memo(({ value, onIncrement }) => { console.log('Render Part'); return <div> <span>{value}</span> <button onClick={onIncrement}>Increment</button> </div>; }); function ComplexUI() { const [partA, setPartA] = useState(0); const [partB, setPartB] = useState(0); const [partC, setPartC] = useState(0); const incA = useCallback(() => setPartA(a => a + 1), []); const incB = useCallback(() => setPartB(b => b + 1), []); const incC = useCallback(() => setPartC(c => c + 1), []); return <div> <Part value={partA} onIncrement={incA} /> <Part value={partB} onIncrement={incB} /> <Part value={partC} onIncrement={incC} /> </div>; }
function ComplexUI() { const [state, setState] = React.useState({ partA: 0, partB: 0, partC: 0 }); // Updating any part causes full component re-render return <div> <button onClick={() => setState({ ...state, partA: state.partA + 1 })}>Update A</button> <button onClick={() => setState({ ...state, partB: state.partB + 1 })}>Update B</button> <button onClick={() => setState({ ...state, partC: state.partC + 1 })}>Update C</button> <div>{state.partA}</div> <div>{state.partB}</div> <div>{state.partC}</div> </div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single state object for all UI parts | High (full component re-render) | Multiple per update | High (all parts repaint) | [X] Bad |
| Split state with memoized components | Low (only updated parts) | Single per update | Low (minimal repaint) | [OK] Good |