Performance: Using props in JSX
Using props in JSX affects how React components render and update, impacting interaction responsiveness and rendering speed.
function Parent() { const data = React.useMemo(() => ({ name: 'Alice' }), []); return <Child info={data} />; } function Child({ info }) { return <div>{info.name}</div>; }
function Parent() { const data = { name: 'Alice' }; return <Child info={{ ...data }} />; } function Child({ info }) { return <div>{info.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline new object props | Multiple due to re-renders | Multiple reflows triggered | Higher paint cost due to frequent updates | [X] Bad |
| Memoized stable props | Minimal DOM updates | Single or minimal reflows | Lower paint cost with fewer updates | [OK] Good |
props in React components?title inside JSX?{props.title}.props object is passed to the component function, so access via {props.title}. Using {title} requires destructuring like function Comp({title}).function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;
}
// Usage:
<Greeting name="Alice" />Greeting component receives a prop called name with value "Alice".{props.name} which inserts the string "Alice" inside the <h2> tag.function Welcome(props) {
return <h1>Welcome, props.name!</h1>;
}props.name as plain text inside JSX, so it will render literally instead of the prop value.props.name, it must be wrapped in curly braces like {props.name}.Button component that shows different text based on a label prop. Which code correctly uses props to achieve this?function Button(props) {
return <button>???</button>;
}???.props.label.props.label in curly braces: {props.label}.Pass the test and this pattern moves unknown