Performance: Passing props to components
Passing props affects rendering speed and update efficiency of React components.
function Parent() { const name = 'Alice'; return <Child name={name} />; } function Child({name}) { return <div>{name}</div>; }
function Parent() { const data = {name: 'Alice', age: 30, city: 'NYC'}; return <Child {...data} />; } function Child(props) { return <div>{props.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing all props indiscriminately | Same DOM nodes reused | Multiple re-renders triggered | Paint cost depends on re-rendered nodes | [X] Bad |
| Passing only necessary props | Same DOM nodes reused | Minimal re-renders | Lower paint cost due to fewer updates | [OK] Good |
props in React components?title with value "Hello" to a component Header?name="value" or name={expression}.function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage:
<Greeting name="Alice" />props.name to display the name.name="Alice", so props.name is "Alice".function Welcome(props) {
return <h2>Welcome, {props.user}!</h2>;
}
// Usage:
<Welcome user=John />user="John".user=John without quotes, causing a syntax error.function Child({ message }) {
return <p>Message: {message || 'No message'}</p>;
}
function Parent() {
const msg = '';
return <Child message={msg} />;
}msg which is an empty string '' as the message prop.{message || 'No message'}. Since empty string is falsy, it falls back to 'No message'.Pass the test and this pattern moves unknown