Performance: Common lifting state patterns
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed by controlling how often components re-render when state changes.
function Parent() { const [count, setCount] = React.useState(0); return ( <> <ChildA setCount={setCount} /> <ChildB count={count} /> </> ); } const ChildA = React.memo(({ setCount }) => { console.log('ChildA rendered'); return <button onClick={() => setCount(c => c + 1)}>Increment</button>; }); const ChildB = React.memo(({ count }) => { console.log('ChildB rendered'); return <div>{count}</div>; });
function Parent() { const [count, setCount] = React.useState(0); return ( <> <ChildA count={count} setCount={setCount} /> <ChildB count={count} setCount={setCount} /> </> ); } function ChildA({ count, setCount }) { return <button onClick={() => setCount(count + 1)}>Increment</button>; } function ChildB({ count }) { return <div>{count}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Lifting state too high | Many components re-render | Multiple reflows per update | High paint cost due to many updates | [X] Bad |
| Lifting state minimally | Only necessary components re-render | Single reflow per update | Low paint cost | [OK] Good |