Performance: State vs props comparison
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 |
state and props in React?state isState is data managed inside a component that can change over time.props areProps are data passed from a parent component to a child component and cannot be changed by the child.State is internal and can change; props are external and read-only. -> Option Btitle with value "Hello" to a child component Header?title="Hello".function Counter({ initial }) {
const [count, setCount] = React.useState(initial);
return <div>Count: {count}</div>;
}
// Usage:
<Counter initial={5} />initial is passed with value 5 to the Counter component.count is initialized with initial, so count starts at 5.function Message(props) {
props.text = "Hello";
return <div>{props.text}</div>;
}props.text = "Hello" tries to modify props, which is not allowed.state and props?Pass the test and this pattern moves unknown