Props Destructuring in React JS – Complete Detailed Notes
Course: React JS Training
Topic: Props Destructuring
Learning Objectives: Understand JavaScript object destructuring, extract props in functional components, assign default values, destructure nested objects and arrays, use callback functions, apply the rest operator, and build reusable React components.
Official Documentation: React – Passing Props to a Component
1. Introduction to Props Destructuring
Props destructuring is a JavaScript feature used in React to extract individual properties from the props object and make them available as local variables. It helps developers write cleaner, shorter, and more readable functional components.
Props, short for properties, allow a parent component to pass information to a child component. This information may include strings, numbers, booleans, arrays, objects, functions, or JSX elements.
Without destructuring, developers commonly access values using props.name, props.age, or props.course. With destructuring, these values can be extracted directly from the props object.
Example Without Props Destructuring
function Student(props) {
return (
<div>
<h2>Name: {props.name}</h2>
<p>Age: {props.age}</p>
<p>Course: {props.course}</p>
</div>
);
}
function App() {
return (
<Student name="Rahul" age={22} course="React JS" />
);
}
export default App;
In this example, the component accesses every value through the props object. The code works correctly, but repeating props. can become unnecessary when a component uses several properties.
Example With Props Destructuring
function Student({ name, age, course }) {
return (
<div>
<h2>Name: {name}</h2>
<p>Age: {age}</p>
<p>Course: {course}</p>
</div>
);
}
function App() {
return (
<Student name="Rahul" age={22} course="React JS" />
);
}
export default App;
Here, name, age, and course are extracted from the props object directly in the function parameter. The component can use these variables without repeatedly accessing props.
2. Understanding JavaScript Object Destructuring
Object destructuring is an ES6 JavaScript feature that allows properties of an object to be assigned to variables using a concise syntax.
Example
const employee = {
name: "Amit",
age: 25,
department: "Development"
};
const { name, age, department } = employee;
console.log(name);
console.log(age);
console.log(department);
Output:
Amit
25
Development
Instead of accessing each property separately, destructuring extracts the values into variables. React uses this same JavaScript feature when components receive props.
How It Works in React
function Employee({ name, department }) {
return (
<h2>{name} works in {department}</h2>
);
}
The expression { name, department } inside the function parameter extracts those properties from the object passed to the component.
3. Understanding the Props Object
When React renders a component, it provides the component with a props object containing the values passed by its parent.
Example
function Welcome(props) {
return <h1>Welcome, {props.username}!</h1>;
}
function App() {
return <Welcome username="Priya" />;
}
export default App;
The child component receives an object conceptually similar to the following:
{
username: "Priya"
}
Using destructuring, the component can be rewritten as follows:
function Welcome({ username }) {
return <h1>Welcome, {username}!</h1>;
}
Both implementations produce the same result. Destructuring changes how the code accesses the data, not how React passes the props.
4. Different Ways to Destructure Props
4.1 Destructuring in the Function Parameter
This is a common approach for functional components. The required properties are extracted directly when the function receives its props object.
function Product({ name, price }) {
return (
<div>
<h2>{name}</h2>
<p>Price: ₹{price}</p>
</div>
);
}
function App() {
return <Product name="Keyboard" price={1200} />;
}
export default App;
Advantages:
- Reduces repetitive property access.
- Makes the component's data requirements easy to identify.
- Improves readability for components using selected props.
- Works well with reusable components.
4.2 Destructuring Inside the Function Body
You can receive the complete props object first and extract the properties inside the function body.
function Product(props) {
const { name, price } = props;
return (
<div>
<h2>{name}</h2>
<p>Price: ₹{price}</p>
</div>
);
}
This approach is useful when the component needs both the entire props object and some individual properties.
4.3 Accessing Props Without Destructuring
function Product(props) {
return (
<div>
<h2>{props.name}</h2>
<p>Price: ₹{props.price}</p>
</div>
);
}
This is also valid React code. Props destructuring is optional, not mandatory.
5. Destructuring Multiple Props
A component can receive several properties from its parent. You can extract all the properties required by the component in a single declaration.
function Course({ title, duration, instructor, fee }) {
return (
<div>
<h2>{title}</h2>
<p>Duration: {duration}</p>
<p>Instructor: {instructor}</p>
<p>Fee: ₹{fee}</p>
</div>
);
}
function App() {
return (
<Course
title="React JS Training"
duration="3 Months"
instructor="Amit Sharma"
fee={15000}
/>
);
}
export default App;
In this example, the component extracts four properties:
title stores the course name.
duration stores the course duration.
instructor stores the instructor's name.
fee stores the numeric course fee.
6. Props Destructuring With Default Values
Some props are optional. JavaScript allows default values to be assigned during destructuring. The default is used when a property is missing or its value is undefined.
Example
function Greeting({ name = "Guest", city = "Mumbai" }) {
return (
<div>
<h2>Hello, {name}!</h2>
<p>City: {city}</p>
</div>
);
}
function App() {
return (
<div>
<Greeting name="Neha" city="Pune" />
<Greeting name="Amit" />
<Greeting />
</div>
);
}
export default App;
Expected results:
- The first greeting displays Neha and Pune.
- The second greeting displays Amit and Mumbai.
- The third greeting displays Guest and Mumbai.
Important: A default value does not replace null or other explicitly supplied values. For example, name={null} remains null; name={undefined} uses the default.
7. Destructuring String, Number, and Boolean Props
Destructuring works with props of different data types. The component can receive text, numeric values, and boolean flags.
function UserProfile({ name, age, isStudent }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
<p>Student: {isStudent ? "Yes" : "No"}</p>
</div>
);
}
function App() {
return (
<UserProfile
name="Karan"
age={21}
isStudent={true}
/>
);
}
export default App;
The component extracts the string name, number age, and boolean isStudent. The boolean value determines which text is displayed.
8. Destructuring Object Props
Sometimes a parent component passes an entire object as a prop. You can extract the object property and access its inner values.
Example 1: Destructure the Top-Level Prop
function Employee({ employee }) {
return (
<div>
<h2>{employee.name}</h2>
<p>Department: {employee.department}</p>
</div>
);
}
function App() {
const employeeData = {
name: "Rohit",
department: "IT"
};
return <Employee employee={employeeData} />;
}
export default App;
Example 2: Nested Object Destructuring
function Employee({ employee: { name, department } }) {
return (
<div>
<h2>{name}</h2>
<p>Department: {department}</p>
</div>
);
}
The nested syntax extracts name and department from the employee object. It assumes that employee exists and is an object. If the object might be missing or null, provide a safe default or validate the data before destructuring.
9. Destructuring Array Props
When an array is passed as a prop, you can destructure the array property from the props object and use JavaScript array methods to display its values.
function Skills({ skills }) {
return (
<div>
<h2>My Skills</h2>
<ul>
{skills.map((skill) => (
<li key={skill}>{skill}</li>
))}
</ul>
</div>
);
}
function App() {
const skillList = ["HTML", "CSS", "JavaScript", "React"];
return <Skills skills={skillList} />;
}
export default App;
The skills property is extracted and used with map() to render a list. In this example, each skill is unique and is used as the list key. In real applications, stable unique IDs are preferable when available.
10. Destructuring Function Props
Functions can be passed from parent components to child components. Destructuring allows the child to access a callback function directly.
function Button({ onClick }) {
return (
<button onClick={onClick}>
Click Me
</button>
);
}
function App() {
function showMessage() {
alert("Button clicked!");
}
return <Button onClick={showMessage} />;
}
export default App;
The parent provides the showMessage function through the onClick prop. The child extracts the function and uses it as the event handler.
Pass the function reference as onClick={showMessage}, rather than writing onClick={showMessage()}, which would call the function during rendering.
11. Renaming Variables During Destructuring
JavaScript allows an object property to be assigned to a differently named local variable. This can help avoid naming conflicts or make the code clearer.
const user = {
name: "Anjali",
city: "Mumbai"
};
const { name: userName, city: userCity } = user;
console.log(userName);
console.log(userCity);
Output:
Anjali
Mumbai
Renaming Props in React
function Profile({ name: displayName, city: location }) {
return (
<div>
<h2>{displayName}</h2>
<p>Location: {location}</p>
</div>
);
}
The parent still passes the props as name and city. The component uses the local variable names displayName and location.
12. Using the Rest Operator With Props Destructuring
The object rest operator (...) collects the remaining properties into a new object after selected properties have been extracted.
function Car({ brand, color, ...otherProps }) {
return (
<div>
<h2>{brand}</h2>
<p>Color: {color}</p>
<p>Model: {otherProps.model}</p>
<p>Year: {otherProps.year}</p>
</div>
);
}
function App() {
return (
<Car
brand="Toyota"
color="White"
model="Innova"
year={2025}
/>
);
}
export default App;
Here, brand and color are extracted individually. The remaining properties, model and year, are collected in otherProps.
Use the rest operator carefully when forwarding properties to native HTML elements. Custom component props should not automatically be forwarded to a DOM element unless they are valid and intended for that element.
13. Props Destructuring With Conditional Rendering
Destructured values can be used in conditions to decide which content a component should display.
function LoginStatus({ isLoggedIn, username }) {
return (
<div>
{isLoggedIn ? (
<h2>Welcome back, {username}!</h2>
) : (
<h2>Please log in to continue.</h2>
)}
</div>
);
}
function App() {
return (
<div>
<LoginStatus isLoggedIn={true} username="Rahul" />
<LoginStatus isLoggedIn={false} username="Rahul" />
</div>
);
}
export default App;
The isLoggedIn property determines which message appears, while username supplies the name for the welcome message.
14. Props Destructuring in Reusable Components
Reusable components can display different content based on the props supplied by their parent. Destructuring helps keep these components concise.
function Button({ text, color = "blue", onClick }) {
return (
<button
onClick={onClick}
style={{
backgroundColor: color,
color: "white",
padding: "10px 16px",
border: "none",
cursor: "pointer"
}}
>
{text}
</button>
);
}
function App() {
return (
<div>
<Button text="Save" color="green" onClick={() => alert("Saved")} />
<Button text="Delete" color="red" onClick={() => alert("Deleted")} />
<Button text="Continue" onClick={() => alert("Continuing")} />
</div>
);
}
export default App;
The same component creates three buttons with different labels and colors. When no color is provided, the default color is blue.
15. Props Destructuring With the Children Prop
When JSX content is placed between a component's opening and closing tags, React makes that content available through the special children prop.
function Card({ title, children }) {
return (
<div className="card">
<h2>{title}</h2>
<div>{children}</div>
</div>
);
}
function App() {
return (
<Card title="React Learning">
<p>Learn components and props.</p>
<button>Start Learning</button>
</Card>
);
}
export default App;
The Card component destructures both title and children. This makes it possible to reuse the same wrapper with different nested content.
16. Props Destructuring in a Student Management Project
Consider a student dashboard that displays student names, courses, scores, and enrollment status. Props destructuring makes the student card reusable.
StudentCard.jsx
function StudentCard({ name, course, score, isActive = true }) {
return (
<div className="student-card">
<h2>{name}</h2>
<p>Course: {course}</p>
<p>Score: {score}%</p>
<p>Status: {isActive ? "Active" : "Inactive"}</p>
</div>
);
}
export default StudentCard;
App.jsx
import StudentCard from "./StudentCard";
function App() {
return (
<div>
<h1>Student Dashboard</h1>
<StudentCard name="Priya" course="React JS" score={92} />
<StudentCard name="Aman" course="JavaScript" score={85} isActive={false} />
<StudentCard name="Neha" course="HTML and CSS" score={88} />
</div>
);
}
export default App;
Project explanation:
- The parent component passes student information through props.
- The child component extracts the required values using destructuring.
- The
isActive prop defaults to true when it is not provided.
- The same component displays different students without duplicating its markup.
17. Props Destructuring vs. Normal Props Access
| Feature |
Normal Props Access |
Props Destructuring |
| Syntax |
props.name |
{ name } in the function parameter |
| Readability |
May be repetitive with many properties |
Often more concise |
| Variables |
Properties are accessed through the object |
Properties are extracted into local variables |
| Default values |
Can be handled separately |
Can be assigned directly in destructuring |
| Nested objects |
Accessed through chained properties |
Can use nested destructuring |
| Mandatory? |
No |
No |
| Suitable use |
When the full props object is useful |
When selected props are used directly |
18. Common Mistakes in Props Destructuring
Mistake 1: Treating the First Function Argument as an Individual Prop
// Incorrect if you expect name to contain the name string
function User(name) {
return <h2>{name}</h2>;
}
React passes one props object to a function component. In this example, name receives the whole object, not the value of its name property.
Correct code:
function User({ name }) {
return <h2>{name}</h2>;
}
Mistake 2: Using props After Destructuring Without Declaring It
function User({ name }) {
return <h2>{props.name}</h2>;
}
This fails because props has not been declared in the function. Use the extracted variable or receive the props object explicitly.
Correct code:
function User({ name }) {
return <h2>{name}</h2>;
}
Mistake 3: Confusing Property Names With Variable Names
function User({ name: username }) {
return <h2>{username}</h2>;
}
This is valid. The original prop is named name, while username is the local variable.
Mistake 4: Expecting a Default to Replace Null
function Greeting({ name = "Guest" }) {
return <h2>{name}</h2>;
}
If the parent passes name={null}, the value remains null. A destructuring default applies only when the property is missing or undefined.
Mistake 5: Mutating Props
// Avoid changing a prop object
function Product({ product }) {
product.price = 500;
return <p>{product.price}</p>;
}
Props should be treated as read-only. If a value needs to change, update the source data in the parent or create a separate derived value rather than mutating the prop.
Mistake 6: Destructuring a Missing Nested Object
// This can fail if employee is undefined or null
function Employee({ employee: { name } }) {
return <h2>{name}</h2>;
}
Ensure the nested object exists before using nested destructuring, or provide a safe default when appropriate.
19. Best Practices for Props Destructuring
- Destructure only the properties the component needs.
- Use meaningful names for extracted variables.
- Provide default values for optional props when appropriate.
- Keep nested destructuring simple and readable.
- Remember that destructuring does not validate data or make props mutable.
- Use stable unique keys when rendering lists of components.
- Keep components focused on a clear responsibility.
- Use the full props object when it improves clarity or is genuinely required.
- Avoid forwarding arbitrary custom properties to native HTML elements.
- Consider TypeScript or prop validation when stronger guarantees about prop types and shapes are needed.
20. Practical Exercises
Exercise 1: User Profile
Create a UserProfile component that receives name, age, and city. Destructure the props and display them in a card.
Exercise 2: Product Details
Create a Product component that receives name, price, and category. Set the default category to "General".
Exercise 3: Course Card
Create a CourseCard component that receives title, duration, fee, and isAvailable. Use conditional rendering to display enrollment availability.
Exercise 4: Nested Object
Pass a teacher object containing name and subject. Destructure the nested values and display them.
Exercise 5: Callback Function
Create a button component that destructures an onClick prop and invokes the function when the button is clicked.
Exercise 6: Reusable Profile Cards
Create a reusable profile card that accepts name, role, location, and an optional isAvailable prop. Render several profiles with different values.
21. Interview Questions and Answers
Q1. What is props destructuring in React?
It is a JavaScript technique that extracts individual properties from the props object so components can access them directly.
Q2. Why do developers use props destructuring?
It reduces repetitive property access, improves readability, and makes it easier to see which props a component uses.
Q3. Is props destructuring mandatory in React?
No. Developers can use either props.name or the destructured variable name.
Q4. What is the difference between props and destructured props?
Props are passed to a component as an object. Destructured props are local variables extracted from that object.
Q5. Can default values be used with destructuring?
Yes. For example, function Greeting({ name = "Guest" }) uses Guest when the prop is missing or undefined.
Q6. Can we destructure nested objects?
Yes. Nested destructuring can extract properties from an object inside another prop, provided the nested object exists.
Q7. Can callback functions be destructured from props?
Yes. A callback can be accessed using syntax such as function Button({ onClick }).
Q8. Can destructuring modify props?
No. Destructuring extracts values but does not make props mutable. Props should be treated as read-only.
Q9. What does the rest operator do in props destructuring?
It collects remaining properties into a separate object, as in function Card({ title, ...rest }).
Q10. What happens when a defaulted prop receives null?
The value remains null. The default applies when the property is missing or undefined.
Q11. Can props destructuring be used with the children prop?
Yes. A component can use function Card({ children }) to access JSX or other content passed between its opening and closing tags.
Q12. Does destructuring improve application performance?
Its main benefit is code readability and convenience. Destructuring alone should not be expected to provide a meaningful React rendering performance improvement.
22. Quick Revision Checklist
- Understand what props are and how parent components pass them to children.
- Know how to destructure props in a function parameter.
- Know how to destructure props inside the function body.
- Practice destructuring strings, numbers, booleans, arrays, objects, and callback functions.
- Understand default values and variable renaming.
- Understand nested destructuring and the rest operator.
- Learn how to destructure the
children prop.
- Avoid mutating props directly.
- Build reusable components using destructured props.
- Complete the exercises and revise the interview questions.
23. Conclusion
Props destructuring is an important JavaScript technique used in React functional components. It allows developers to extract required properties from the props object, making component code more concise and readable.
By learning default values, nested destructuring, variable renaming, callback functions, the rest operator, and the children prop, developers can build reusable and maintainable React applications. Remember that destructuring changes how values are accessed, not the way React passes props, and props should always be treated as read-only.
Learn React JS With JustAcademy
Explore the React JS training course to strengthen your understanding of JSX, components, props, state, hooks, routing, and practical application development.
JustAcademy React JS Training Course
Interested in learning more about the course? Register for a demo session using the link below.
Register for React JS Course Demo