Props: passing data to components

Intermediate

Introduction

A component that always shows the same thing is of limited use. A ProfileCard should show whoever it is given. Props are how a parent passes data to a child component, exactly like arguments to a function: the same component, given different props, shows different content.

How it works

You pass props as attributes when you use a component, with strings in quotes and any other value in braces:

<ProfileCard name="Ada" age={36} skills={["maths", "poetry"]} />

React collects them into one object and passes it as the component's first argument. You can read props.name, but it is more common to destructure the object in the parameter list, which also documents which props the component uses:

function ProfileCard({ name, age, role = "Student" }) {
  return <h3>{name}, {age}</h3>;
}

A default value, such as role = "Student", is used when the parent does not pass that prop.

Whatever you write between a component's opening and closing tags arrives as the special prop children. That makes wrapper components easy: a Card, a Panel or a Layout can place its children wherever it likes:

<ProfileCard name="Ada">
  <p>Wrote the first program.</p>
</ProfileCard>

Props flow one way, from parent to child: one-way data flow. A component must treat its props as read-only and never change them; if something needs to change over time, that is state, a later lesson. Because a component's output depends only on its props (and its state), the same props always give the same result, which makes components predictable and easy to test.

Props can hold any value: strings, numbers, arrays, objects, other JSX, and functions. Passing functions is how a child tells its parent that something happened, which you will use with events.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. ProfileCard destructures name, role with a default, and children.
  2. The first card passes a role; the second leaves it out, so "Student" is used.
  3. The paragraphs between the tags arrive as children and are placed in the about div.
  4. One component, two calls, two different cards.

Expected result

The preview shows two cards: "Ada" with the role "Mathematician" and the text "Wrote the first program.", and "Rahim" with the role "Student" and the text "Learning React."

In the real world

Component libraries such as Material UI are collections of components configured entirely through props: a Button takes variant, size, disabled and onClick. Layout components use children to wrap pages. Reading a component's props is usually the fastest way to understand what it does.

Common mistakes

  • Passing a number as a string: age="36" passes text; write age={36}.
  • Changing a prop inside the component, which React does not expect and which leads to bugs.
  • Forgetting to destructure and writing name when only props.name exists.
  • Misspelling a prop in the parent, so the child receives undefined.

Best practices

  • Destructure props in the parameter list, with defaults for optional ones.
  • Keep props few and specific; a component with many props may need splitting.
  • Use children for content that a component only wraps.

Summary

Props pass data from a parent to a child component as one object, which the child usually destructures, with default values for optional props. Content between a component's tags arrives as children. Data flows one way, and a component must never change its props.

Quick check

1. How do you pass the number 36 as a prop?
2. What is the children prop?
3. In function Card({ size = 'large' }), when is 'large' used?
4. May a component change its own props?

Key takeaways

  • Props are a component's arguments: the parent decides them, the child reads them.
  • Destructure props in the parameter list, with defaults for optional ones.
  • children lets a component wrap any content; props are read-only.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.