Chapter 3 · Describing the UI

Props

Props are how a parent component passes information to its children, just like arguments to a function.

You will learn
  • How to pass props to a component
  • How to read props, with destructuring
  • How to set default values
  • How to pass JSX as children
  • Why props are read-only

What are props?

You already use props with HTML tags. In <img src="..." alt="..." />, src and alt are information you pass into the image. Your own components can take props too, and any JavaScript value can be a prop: strings, numbers, objects, arrays, even functions.

Step 1: pass props to the child

The rule from Chapter 2 applies: strings go in quotes (name="Lin"), and everything else goes in braces (size={100}, person={{ ... }}).

Step 2: read props inside the child

React collects all the props into one object and passes it as the component's first argument:

So you can read them like this:

Most React code uses destructuring instead, which pulls the props out by name directly in the parameter list:

Don't forget the braces function Avatar({ person, size }) ✅ destructures the props object.
function Avatar(person, size) ❌ is wrong: person would be the whole props object, and size would be undefined.

Reuse: same component, different props

Think of a component as a template and props as the blanks you fill in. You can change Avatar without touching Profile, and the other way round.

Default values

If a prop might not be passed, give it a default with = in the destructuring:

The default is used when the prop is missing or undefined. It is not used when you pass null or 0.

Forwarding props with spread

Sometimes a component just passes everything along to a child. The spread syntax {...props} forwards them all:

Use this sparingly. If you spread props everywhere, it becomes hard to see which component receives what.

Passing JSX as children

With HTML you can put tags inside other tags: <div><img /></div>. Your components can do the same. Whatever you put between the opening and closing tags arrives as a special prop called children:

Think of Card as a picture frame: it doesn't need to know what's inside, it just draws the border around it. Panels, modals, layouts and grids are commonly built this way.

Props are read-only

A component receives props but must never change them:

Props can still change over time: the parent can re-render with different props, and the child then shows the new values. But it's always the parent that changes them. If a component needs its own changing data (for example, in response to a click), that's state, covered in Chapter 7.

Data flows one way Parent → child, through props. A child can't reach up and change its parent's data directly. You'll learn the proper way (passing a function as a prop) in Chapter 6.

More examples

Example 1: a button with "variants"

One component, many looks. Props with good defaults make components pleasant to use.

Example 2: any value can be a prop

Example 3: a layout built with children

Example 4: computing from props

Recap

  • Pass props like HTML attributes: <Avatar person={p} size={100} />.
  • Read them with destructuring: function Avatar({ person, size }).
  • Defaults: { size = 100 }. Forward everything: {...props}.
  • Nested JSX arrives as children.
  • Props are read-only snapshots. Use state for data that changes.

Exercises

1. Extract a component

The code below repeats itself. Create a Scientist component that takes name, imageId and discovery props, and use it twice.

Show solution

The backticks `...${imageId}...` are a JavaScript template string, an easy way to build text from variables.

2. A frame with children

Create a Panel component that takes a title prop and children, and draws a bordered box with the title on top. Use it to wrap a paragraph.

Show solution

3. Greeting with a default 🟢 easy

Make <Greeting name="Asha" /> show "Hello, Asha!", and <Greeting /> show "Hello, Guest!".

Show solution

4. Badge 🟢 easy

Create <Badge label="New" color="green" />, which renders a small rounded pill with white text on the given background colour.

Show solution

5. Fix the props bug 🟢 easy

Show solution

Props arrive as one object. Without braces, title is the whole props object and body is undefined. Fix: function Card({ title, body }).

6. Product card 🟢 easy

Create ProductCard, which takes one product prop ({ name, price, imageUrl }) and shows the image, the name, and the price formatted like "₹ 499".

Show solution

7. Star rating 🟡 medium

Make <Rating value={3} /> show "★★★☆☆". Use a max prop with a default of 5. Hint: '★'.repeat(n).

Show solution

8. Alert box with children 🟡 medium

Create <Alert type="warning">...</Alert>. The type (info, warning or error) picks the border colour and an icon (ℹ️ ⚠️ ❌), and children is the message.

Show solution

9. Passing a function prop 🟡 medium

Create Greeter, which takes a format function prop and a name, and shows format(name). Render it twice: once with a function that shouts (n => n.toUpperCase() + '!!!'), and once with a function that whispers.

Show solution

10. Smart image size 🔴 hard

The react.dev images come in two sizes: a suffix of s (small) or b (big), e.g. .../YfeOqp2s.jpg. Write a helper getImageUrl(imageId, size) that picks 'b' when size > 90 and 's' otherwise, and use it in an Avatar({ person, size }) component. Render avatars at sizes 40, 70 and 120.

Show solution