Performance: State vs props comparison
MEDIUM IMPACT
This concept affects how React components update and re-render, impacting interaction responsiveness and rendering speed.
function Parent() { const data = { value: 42 }; return <Child data={data} />; } function Child({ data }) { // use props directly without copying to state return <div>{data.value}</div>; }
function Parent() { const [count, setCount] = React.useState(0); const data = { value: 42 }; return <Child data={data} />; } function Child({ data }) { const [localData, setLocalData] = React.useState(data); // copies props to state unnecessarily return <div>{localData.value}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Copy props to state unnecessarily | Extra virtual DOM nodes created | Multiple reflows per update | Higher paint cost due to repeated updates | [X] Bad |
| Use props directly without copying | Minimal DOM operations | Single reflow per update | Lower paint cost | [OK] Good |
| Frequent state updates without memoization | Many DOM updates | Many reflows | High paint cost | [!] Bad |
| State updates with useCallback and memo | Optimized DOM updates | Reduced reflows | Lower paint cost | [OK] Good |