Destructuring Props in Function Parameters
What you'll learn: How to extract prop values directly in your function signature for cleaner, more readable component code.
Instead of writing props.name, props.age, props.email throughout your component, you can "unpack" the values you need right when defining the function.
The Core Idea
When your component receives props, you can use JavaScript destructuring syntax in the function parameter to extract specific properties immediately. This eliminates the need to repeatedly type props. before every property name.
Before destructuring:
function UserCard(props) {
return (
<div>
<h2>{props.name}</h2>
<p>Age: {props.age}</p>
</div>
);
}
After destructuring:
function UserCard({ name, age }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
</div>
);
}
Notice how { name, age } replaces props in the function signature. React still passes the entire props object—you're just extracting the pieces you need upfront.
Think of It Like Unpacking a Box
Imagine props as a delivery box. Without destructuring, you reach into the box every time: box.item1, box.item2. With destructuring, you unpack what you need right when you receive it: { item1, item2 } = box.
This pattern is especially helpful when:
- You use many props throughout your component
- You want clearer code at a glance
- You're working with callback props like
onClickoronSubmit
Key Takeaway: Destructuring props in the function parameters lets you extract values directly from the props object, making your code shorter and more readable by eliminating repetitive props. prefixes.