Performance: Props destructuring
This affects the JavaScript execution speed and readability but has minimal direct impact on page load or rendering speed.
function Greeting({ name, surname }) { return <h1>Hello, {name} {surname}!</h1>; }
function Greeting(props) { return <h1>Hello, {props.name} {props.surname}!</h1>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Access props via props.name | 0 | 0 | 0 | [OK] Good |
| Destructure props in function parameters | 0 | 0 | 0 | [OK] Good |
props destructuring in a React functional component do?name and age in a React functional component?name and age directly in the parameters.{name} is {age}
; } [OK]function Greeting({ name, city }) {
return <p>Hello {name} from {city}!</p>;
}
// Usage: <Greeting name="Anna" city="Paris" />name and city from props directly.name="Anna" and city="Paris", the JSX renders as <p>Hello Anna from Paris!</p>.function Info({ title, description }) {
return <div>
<h1>{title}</h1>
<p>{props.description}</p>
</div>
}title and description from props, so props is not defined inside.props.description causes an error because props is not declared; should use description directly.firstName, lastName, and age. You receive props as a single object. Which is the best way to use props destructuring to make your code clean and readable?Pass the test and this pattern moves unknown