Props Destructuring
Learn how to extract values from props using JavaScript destructuring and write cleaner React functional components.
01 What Is Props Destructuring?
Props destructuring is a JavaScript technique that allows you to
extract individual properties from the props object. Instead of
repeatedly writing props.name, you can use
name directly.
Props properties can be extracted into individual variables.
02 Using Props Without Destructuring
First, see how a component accesses each value using the
props object.
function Student(props) {
return (
<div>
<h2>{props.name}</h2>
<p>Course: {props.course}</p>
<p>City: {props.city}</p>
</div>
);
}
export default Student;
Samadhan
Course: React JS
City: Pune
03 Destructuring Inside the Component
You can destructure props inside the component body by using an assignment statement.
function Student(props) {
const { name, course, city } = props;
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default Student;
The statement const { name, course, city } = props;
extracts these three properties into variables.
Samiksha
Course: Full Stack Development
City: Pune
04 Destructuring Directly in Function Parameters
React functional components can destructure props directly in their function parameters. This is a common and concise pattern.
function Student({ name, course, city }) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default Student;
Pradnya
Course: Python
City: Pune
05 Passing Props from the Parent Component
The parent component passes values using JSX attributes. The child receives those values and destructures them.
function App() {
return (
<Student
name="Samadhan"
course="React JS"
city="Pune"
/>
);
}
function Student({ name, course, city }) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default App;
Samadhan
Course: React JS
City: Pune
06 Destructuring with Default Values
You can provide default values during destructuring. A default is
used when the property is missing or its value is
undefined.
function Student({
name = "Guest",
course = "Not Assigned"
}) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
</div>
);
}
export default Student;
When the parent renders <Student />, the defaults are used.
Guest
Course: Not Assigned
07 Destructuring Array and Object Props
You can destructure an object prop and then use its properties. Array props can also be received and processed in the component.
function App() {
const student = {
name: "Samiksha",
course: "React JS"
};
const skills = ["HTML", "CSS", "JavaScript"];
return (
<Student details={student} skills={skills} />
);
}
function Student({ details, skills }) {
const { name, course } = details;
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<h3>Skills
Samiksha
Course: React JS
Skills
- HTML
- CSS
- JavaScript
08 Destructuring in Reusable Components
Destructuring makes reusable components easier to read because each component can access its required properties directly.
function App() {
return (
<div>
<Course name="Java" duration="4 Months" />
<Course name=".NET" duration="5 Months" />
<Course name="Python" duration="3 Months" />
</div>
);
}
function Course({ name, duration }) {
return (
<div>
<h3>{name}</h3>
<p>Duration: {duration}</p>
</div>
);
}
export default App;
Java
Duration: 4 Months
.NET
Duration: 5 Months
Python
Duration: 3 Months
09 Props Destructuring vs props.property
Both approaches access the same prop values. Destructuring is often shorter when a component needs multiple properties.
// Without destructuring
function StudentA(props) {
return <p>{props.name} - {props.course}</p>;
}
// With destructuring
function StudentB({ name, course }) {
return <p>{name} - {course}</p>;
}
Samadhan - React JS
Samadhan - React JS
Both approaches produce the same output when given the same props.
10 Important Rules
- Destructuring is a JavaScript feature.
- You can destructure props inside the component or in its parameters.
- Property names must match the names in the props object.
- Default values can be provided during destructuring.
- Destructuring does not make props mutable.
- Use destructuring when it makes your component easier to read.
This section describes React coding rules; it does not produce a separate rendered component.
Summary
Props destructuring is a JavaScript feature that extracts properties from an object into individual variables. In React, it lets functional components access props directly without repeatedly writing props.property. You can destructure strings, numbers, objects and arrays received through props, and you can provide default values for missing or undefined properties. Destructuring improves readability while keeping props read-only and supporting reusable components at CIIT Training Institute.