Lesson 19 of 60 – Props Destructuring
32%

Props Destructuring

Props destructuring is a JavaScript technique that allows you to extract individual properties directly from the props object. It makes React components cleaner and easier to read.

Note: Instead of repeatedly writing props.name, props.age, or props.course, you can destructure these properties directly inside the component parameter.

1. What is Props Destructuring?

Props destructuring means extracting properties from the props object and using them directly inside the component.

Without destructuring:

function Student(props) {

    return (
        <h2>
            {props.name}
        </h2>
    );

}

With destructuring:

function Student({ name }) {

    return (
        <h2>
            {name}
        </h2>
    );

}

2. Why Use Props Destructuring?

Destructuring makes component code shorter and more readable.

  • Reduces repeated props. syntax.
  • Makes JSX easier to read.
  • Clearly shows which props the component uses.
  • Makes component parameters easier to understand.
  • Works naturally with JavaScript.

3. Basic Props Destructuring

Suppose a component receives a name prop.

function Student({ name }) {

    return (
        <h2>
            {name}
        </h2>
    );

}

The name property is extracted from the props object.

4. Passing the Prop

function App() {

    return (
        <Student name="Rahul" />
    );

}

The value Rahul is passed to the Student component through the name prop.

5. Destructuring Multiple Props

Multiple properties can be destructured at the same time.

function Student({ name, age, course }) {

    return (
        <div>

            <h2>{name}</h2>

            <p>Age: {age}</p>

            <p>Course: {course}</p>

        </div>
    );

}

6. Passing Multiple Props

function App() {

    return (
        <Student
            name="Neha"
            age={20}
            course="React"
        />
    );

}

The child component receives all three values.

7. Destructuring vs Normal Props

Without destructuring:

function Course(props) {

    return (
        <div>

            <h2>{props.name}</h2>

            <p>{props.duration}</p>

            <p>{props.fee}</p>

        </div>
    );

}

With destructuring:

function Course({ name, duration, fee }) {

    return (
        <div>

            <h2>{name}</h2>

            <p>{duration}</p>

            <p>{fee}</p>

        </div>
    );

}

8. Destructuring in Function Parameters

The most common approach is to destructure props directly in the function parameter.

function Welcome({ name }) {

    return (
        <h1>
            Welcome {name}
        </h1>
    );

}

9. Destructuring Inside the Function

You can also receive the complete props object and destructure it inside the function.

function Student(props) {

    const { name, age } = props;

    return (
        <div>

            <h2>{name}</h2>

            <p>Age: {age}</p>

        </div>
    );

}

10. Destructuring String Props

function Greeting({ name }) {

    return (
        <h2>
            Hello {name}
        </h2>
    );

}
<Greeting name="Amit" />

11. Destructuring Number Props

Number values can also be destructured.

function Student({ age }) {

    return (
        <p>
            Age: {age}
        </p>
    );

}
<Student age={21} />

12. Destructuring Boolean Props

function User({ isActive }) {

    return (
        <p>
            Status:
            {isActive ? " Active" : " Inactive"}
        </p>
    );

}
<User isActive={true} />

13. Destructuring Array Props

Arrays can be destructured as properties of the props object.

function Subjects({ subjects }) {

    return (
        <ul>

            {subjects.map(subject => (
                <li key={subject}>
                    {subject}
                </li>
            ))}

        </ul>
    );

}
<Subjects
    subjects={["HTML", "CSS", "React"]}
/>

14. Destructuring Object Props

An object can be passed as a prop and then used inside the component.

function Student({ student }) {

    return (
        <div>

            <h2>
                {student.name}
            </h2>

            <p>
                Age: {student.age}
            </p>

        </div>
    );

}

15. Nested Destructuring

Nested object properties can also be destructured.

function Student({ student: { name, age } }) {

    return (
        <div>

            <h2>{name}</h2>

            <p>Age: {age}</p>

        </div>
    );

}

16. Destructuring with Default Values

A default value can be provided when a prop is not supplied.

function Welcome({
    name = "Student"
}) {

    return (
        <h2>
            Welcome {name}
        </h2>
    );

}

If no name prop is passed, the component uses Student.

17. Multiple Default Values

function Course({
    name = "React",
    duration = "3 Months",
    fee = 5000
}) {

    return (
        <div>

            <h2>{name}</h2>

            <p>Duration: {duration}</p>

            <p>Fee: ₹{fee}</p>

        </div>
    );

}

18. Destructuring Function Props

Functions passed through props can also be destructured.

function Button({ onClick }) {

    return (
        <button onClick={onClick}>
            Click Me
        </button>
    );

}

19. Passing a Function Prop

function App() {

    function handleClick() {
        alert("Hello!");
    }

    return (
        <Button
            onClick={handleClick}
        />
    );

}

The child component can use the destructured onClick function.

20. Destructuring the children Prop

The special children prop can also be destructured.

function Card({ children }) {

    return (
        <div className="card">
            {children}
        </div>
    );

}
<Card>

    <h2>React Course</h2>

</Card>

21. Destructuring Props with JSX

Destructured variables can be directly used inside JSX.

function Profile({ name, role }) {

    return (
        <div>

            <h2>{name}</h2>

            <p>
                Role: {role}
            </p>

        </div>
    );

}

22. Destructuring Makes JSX Cleaner

Without destructuring:

<h2>{props.name}</h2>

<p>{props.course}</p>

<p>{props.duration}</p>

With destructuring:

<h2>{name}</h2>

<p>{course}</p>

<p>{duration}</p>

The second version is shorter and easier to read.

23. Destructuring and Component Reusability

function Course({
    name,
    duration,
    fee
}) {

    return (
        <div className="course-card">

            <h2>{name}</h2>

            <p>
                Duration: {duration}
            </p>

            <p>
                Fee: ₹{fee}
            </p>

        </div>
    );

}
<Course
    name="React"
    duration="6 Months"
    fee={10000}
/>

<Course
    name="Python"
    duration="6 Months"
    fee={8000}
/>

24. Props Destructuring in Arrow Functions

Destructuring works with arrow function components as well.

const Student = ({ name, age }) => {

    return (
        <div>

            <h2>{name}</h2>

            <p>Age: {age}</p>

        </div>
    );

};

25. Destructuring with Conditional Rendering

function UserStatus({ isLoggedIn, name }) {

    return (
        <div>

            {isLoggedIn ? (
                <h2>
                    Welcome {name}
                </h2>
            ) : (
                <h2>
                    Please Login
                </h2>
            )}

        </div>
    );

}

26. Destructuring with Lists

function StudentList({ students }) {

    return (
        <ul>

            {students.map(student => (
                <li key={student.id}>
                    {student.name}
                </li>
            ))}

        </ul>
    );

}

The students array is extracted directly from props.

27. Destructuring Props with State

Props can be destructured in a component that also uses state.

import { useState } from "react";

function Counter({ title }) {

    const [count, setCount] = useState(0);

    return (
        <div>

            <h2>{title}</h2>

            <p>
                Count: {count}
            </p>

            <button
                onClick={() => setCount(count + 1)}
            >
                Increase
            </button>

        </div>
    );

}

28. Common Mistake in Props Destructuring

When destructuring, the property name must match the prop name unless you intentionally rename it.

function Student({ name }) {

    return (
        <h2>{name}</h2>
    );

}

If the parent sends studentName instead of name, the component will not receive the expected property.

<Student
    studentName="Rahul"
/>

29. Complete Props Destructuring Example

function StudentCard({
    name,
    age,
    course,
    city
}) {

    return (
        <div className="card">

            <h2>
                {name}
            </h2>

            <p>
                Age: {age}
            </p>

            <p>
                Course: {course}
            </p>

            <p>
                City: {city}
            </p>

        </div>
    );

}

function App() {

    return (
        <div>

            <StudentCard
                name="Rahul"
                age={21}
                course="React"
                city="Patna"
            />

            <StudentCard
                name="Neha"
                age={20}
                course="JavaScript"
                city="Delhi"
            />

        </div>
    );

}

export default App;

The same component is reused with different values, and all props are destructured directly in the component parameter.

30. Props Destructuring Best Practices

  • Destructure only the props that the component actually uses.
  • Use meaningful prop names.
  • Use default values when appropriate.
  • Keep destructuring readable when there are many props.
  • Remember that destructuring does not change the original props.
  • Do not directly modify props.
  • Use destructuring with functional and arrow function components.
  • Use the special children prop for nested content.
  • Use functions as props when child components need to trigger parent behavior.
  • Keep components simple and reusable.

📌 Key Points

  • Props destructuring extracts properties from the props object.
  • It reduces repeated props.propertyName syntax.
  • Props can be destructured directly in function parameters.
  • Props can also be destructured inside the component body.
  • Multiple props can be destructured together.
  • Default values can be provided during destructuring.
  • Arrays, objects, booleans, numbers, strings, and functions can be destructured.
  • The children prop can also be destructured.
  • Destructuring makes React components easier to read.
  • Destructuring is a JavaScript feature commonly used with React props.

🧠 Quick Quiz

Question: Which syntax is used to destructure props directly inside a React functional component parameter?