# Props & Destructuring

> How to pass data into components and unpack it cleanly using JavaScript destructuring.

Subject: React | Lesson 7 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/props-and-destructuring
Previous: [Functional Components](https://codewithanimation.in/react/functional-components.md) | Next: [State & useState](https://codewithanimation.in/react/state-basics.md)

## What they are

**Props** (short for properties) are the mechanism for passing data from a parent component down to a child component. They are the React equivalent of HTML attributes.

Every React Functional Component receives exactly one argument: a single JavaScript object containing all the props passed to it. **Destructuring** is a standard JavaScript feature used to unpack properties from that object directly in the function signature.

## Why it matters

Props allow you to create dynamic, reusable components. Instead of hardcoding a user's name into a `Profile` component, you pass the name as a prop, allowing you to reuse the exact same component for hundreds of different users.

Destructuring makes your code significantly cleaner. Instead of typing `props.name` and `props.age` repeatedly, you unpack them immediately into `name` and `age` variables.

## How it works

When a parent renders `<Avatar image="/me.png" size={50} />`, React bundles those attributes into a single object: `{ image: '/me.png', size: 50 }`.

It then passes that object as the first argument to the `Avatar` function. You can receive it as `function Avatar(props)` and use `props.image`, or you can destructure it immediately: `function Avatar({ image, size })`.

## Common mistake: The missing curly braces trap

A very common beginner mistake is writing `function Avatar(image) { ... }`. In this case, `image` is not a string; it is the entire props object!

If you try to render `{image}`, React will throw an error because you are trying to render a full object into the DOM. You must wrap the parameter in curly braces to destructure it: `function Avatar({ image })`.

## Tip: Default Props

Because destructuring is just standard JavaScript, you can easily assign default values right in the function signature: `function Avatar({ size = 100 })`. If the parent forgets to pass `size`, it will safely default to 100.

## Try it: Reusable Blueprints

Notice how the exact same component is rendered twice, but produces different UI because the parent passed different props.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/props-and-destructuring

## Check yourself

### Question 1

How many arguments does a React Functional Component receive?

- A. Zero arguments.
- B. Exactly one argument: the props object.
- C. One argument for every prop passed in.

Correct answer: B

No matter how many props you pass, React always bundles them into a single object and passes that object as the first and only argument.

- Why not A: It receives the props object.
- Why not C: React bundles all props into a single object.

### Question 2

If a parent renders `<Button label="Submit" />`, what is wrong with this component definition? `function Button(label) { return <button>{label}</button>; }`

- A. Nothing is wrong, this is correct syntax.
- B. The parameter should be enclosed in curly braces to destructure the object: `function Button({ label })`
- C. The `label` should be accessed via `this.label`.

Correct answer: B

Without curly braces, the first argument is the entire props object (`{ label: 'Submit' }`). Attempting to render an object directly in JSX causes a crash.

- Why not A: This will crash because `label` is the entire props object.
- Why not C: Functional components do not use `this`.

### Question 3

How can you provide a default value for a prop if the parent doesn't provide one?

- A. Use standard JavaScript default parameters in the destructuring signature.
- B. Use the `React.setDefault()` method.
- C. Props cannot have default values; they must always be provided by the parent.

Correct answer: A

You can simply write `function Child({ name = 'Guest' })` to safely fall back to a default value if the prop is omitted.

- Why not B: This method does not exist.
- Why not C: Standard JS allows default values during destructuring.

## Remember this

- Components receive exactly one argument: the props object.
- Use `{ destructuring }` to cleanly unpack props in the function signature.
- Don't forget the curly braces in your function parameters, or you will accidentally capture the entire object instead of the property.
- You can assign default values directly in the destructuring signature.
