Performance: What are props
Props affect how components receive data and can impact rendering performance when changed frequently or deeply nested.
function Parent() { const data = { name: 'Alice', age: 30 }; return <Child info={data} />; } function Child({ info }) { return <div>{info.name}</div>; }
function Parent() { const data = { name: 'Alice', age: 30 }; return <Child info={JSON.stringify(data)} />; } function Child({ info }) { const parsed = JSON.parse(info); return <div>{parsed.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing serialized props | Extra JS parsing | Triggers re-render | Higher paint cost | [X] Bad |
| Passing direct props | Minimal JS work | Only necessary re-render | Lower paint cost | [OK] Good |
props in React?title with value "Hello" to a component Greeting?title="Hello". misses the key, C uses unquoted value, D misses quotes.function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage:
<Welcome name="Alice" />props.name inside JSX to display the passed name.name="Alice", so props.name equals "Alice".function Message(props) {
return <p>{props.text}</p>;
}
// Usage:
<Message text=Hello />text=Hello is missing quotes around the string value.text="Hello" or wrapped in curly braces if a variable.Button component that shows different labels based on props. Which approach correctly uses props to achieve this?function Button(props) {
return <button>{props.label}</button>;
}
// Usage:
<Button label="Save" />
<Button label="Cancel" />Button component uses props.label inside the button, and different labels are passed in usage.Pass the test and this pattern moves unknown