Destructuring Props and Default Prop Values0%

Destructuring Props and Default Prop Values

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Destructuring Props and Default Prop Values

1. The Power of Prop Destructuring

In standard JavaScript functions, accessing properties repeatedly through an object parameter (props.title, props.subtitle, props.onClick) creates verbose, repetitive code.

Modern React applications universally leverage ES6 Object Destructuring directly within the component function parameter signature:

Without Destructuring:

JSX
function CourseHeader(props) {
return (
<header className="course-header">
<h1>{props.title}</h1>
<p>{props.author}</p>
<span>{props.rating} ★</span>
</header>
);
}

With Parameter Destructuring:

JSX
function CourseHeader({ title, author, rating }) {
return (
<header className="course-header">
<h1>{title}</h1>
<p>{author}</p>
<span>{rating} ★</span>
</header>
);
}

Destructuring makes the component's contract immediately obvious at a glance: any developer inspecting the function header can immediately see all the props the component expects.

2. Providing Default Prop Values

When building reusable design system components, certain props should have sensible defaults if the consumer fails to provide them.

You can assign ES6 default parameter values directly inside the destructuring pattern:

JSX
export default function Button({
children,
variant = 'primary', // Defaults to 'primary' if omitted
size = 'md', // Defaults to 'md' if omitted
disabled = false, // Defaults to false if omitted
onClick
}) {
return (
<button
className={`btn btn-${variant} btn-${size}`}
disabled={disabled}
onClick={onClick}
>
{children}
</button>
);
}

Usage:

JSX
// Uses defaults: variant='primary', size='md', disabled=false
<Button onClick={handleSave}>Save</Button>
 
// Overrides specific defaults
<Button variant="danger" size="lg" disabled={isSubmitting}>
Delete Account
</Button>

Note: A default value is applied if the incoming prop is undefined or omitted entirely. If the parent explicitly passes null, the default value is not applied, because null is an intentional value in JavaScript.

3. Rest Props and Prop Spreading

When creating wrapper components (such as a custom <TextInput> or <Card>), you frequently want to extract a few specific props and forward all remaining standard HTML attributes (id, placeholder, aria-*, onFocus) to the underlying element.

You can combine destructuring with the ES6 Rest syntax (...rest) and JSX Spread ({...rest}):

JSX
export default function TextInput({ label, error, ...restProps }) {
return (
<div className="input-group">
{label && <label className="form-label">{label}</label>}
<input
className={`form-input ${error ? 'input-error' : ''}`}
{...restProps} // Spreads placeholder, value, onChange, type, disabled, etc.
/>
{error && <span className="error-text">{error}</span>}
</div>
);
}

Usage:

JSX
<TextInput
label="Corporate Email"
type="email"
placeholder="sumit@mskinstitute.com"
autoComplete="email"
required
error={emailError}
value={email}
onChange={handleEmailChange}
/>

Interactive Quiz

Practice Quiz

Question 1
What JavaScript ES6 feature allows extracting specific prop keys directly inside a component's function parameters?
Question 2
In the component function Avatar({ size = 50 }), what value will size receive if the parent calls <Avatar size={undefined} />?
Question 3
What value will size receive if the parent calls <Avatar size={null} /> in the previous question?
Question 4
What does the JSX spread syntax <input {...restProps} /> accomplish?
Question 5
Why is prop destructuring widely considered a best practice in modern React?
Next Lesson

Passing Children and Composing Components

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext