Performance: Why lifting state is needed
MEDIUM IMPACT
This affects how efficiently React components update and share data, impacting interaction responsiveness and rendering speed.
function Parent() { const [count, setCount] = React.useState(0); return ( <> <ComponentA count={count} setCount={setCount} /> <ComponentB count={count} setCount={setCount} /> </> ); } // State is lifted and shared, keeping data consistent
function Parent() { const [countA, setCountA] = React.useState(0); const [countB, setCountB] = React.useState(0); return ( <> <ComponentA count={countA} setCount={setCountA} /> <ComponentB count={countB} setCount={setCountB} /> </> ); } // ComponentA and ComponentB cannot share state easily
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated state in multiple parents | Multiple state updates causing many DOM changes | Triggers reflows for each component update | Higher paint cost due to frequent updates | [X] Bad |
| Lifted state in common ancestor | Single state update triggers minimal DOM changes | Single reflow per update | Lower paint cost with batched updates | [OK] Good |