Learn Props in React

Props are data that a parent component (the one that uses another component) sends to a child component (the one being used). You write them like attributes…

The Greeting component from the previous lesson always shows the same text: Welcome!. Three tags give three identical paragraphs. But product cards in an online shop each say something different: another title, another price. How does one component show different content each time it's used?

Through props: the data a parent component sends to a child component. You send them the way you write HTML attributes: <Welcome name="Andi" />. Think of a coffee order form. The form is the same for every customer, the answers (name, size, sugar) differ, and the barista makes the coffee from those answers. The barista doesn't cross out what the customer wrote. If the order changes, the customer fills in a new form. In React, the customer is the parent, the answers on the form are the props, and the barista is the child component.

Send from the parent, receive in the child

In this code, App sends a different name to each of two Welcome components:

function Welcome({ name }) {
  return <h2>Hello, {name}!</h2>
}

function App() {
  return (
    <div>
      <Welcome name="Andi" />
      <Welcome name="Sari" />
    </div>
  )
}

This table shows what Welcome receives from each tag:

Tag in App props that Welcome receives Result
<Welcome name="Andi" /> { name: "Andi" } <h2>Hello, Andi!</h2>
<Welcome name="Sari" /> { name: "Sari" } <h2>Hello, Sari!</h2>

Many props at once

The ProductCard component below receives three props: title, price and shipping:

function ProductCard({ title, price, shipping = 10 }) {
  return (
    <div className="card">
      <h3>{title}</h3>
      <span className="price">{price}</span>
      <small>Total + shipping: {price + shipping}</small>
    </div>
  )
}

function App() {
  return (
    <div>
      <ProductCard title="Laptop" price={750} />
      <ProductCard title="Mouse" price={15} />
      <ProductCard title="Headset" price={35} shipping={0} />
    </div>
  )
}
Card props Total + shipping
Laptop { title: "Laptop", price: 750 } 760
Mouse { title: "Mouse", price: 15 } 25
Headset { title: "Headset", price: 35, shipping: 0 } 35

Text in quotes, every other value in curly braces

Take a guess first. App writes <ProductCard title="Mouse" price="15" />, and inside the card there is {price + shipping}, where shipping is 10. Does it show 25, or something else?

Every value other than text is sent with curly braces:

<ProductCard
  title="Laptop"
  price={750}
  inStock={true}
  tags={["promo", "new"]}
  shipping={cityShipping}
/>

Props are read-only

Props belong to the parent. The child only reads them and never changes them. If ProductCard needs another title, App sends a new title. How App changes its data is covered a few lessons later, in the lessons about state (data that can change) and events (things that happen, such as a click).

The code below tries to change props inside the child:

function ProductCard(props) {
  props.title = "Promo" // wrong
  return <h3>{props.title}</h3>
}

⚠️ Props that arrive wrong, or not at all

TL;DR: Props are data that a parent component (the one that uses another component) sends to a child component (the one being used). You write them like attributes: <Welcome name="Andi" />. React gathers them into one props object. The child reads it with destructuring, function Welcome({ name }), which takes name straight out of that object, or with props.name. Text goes in quotes. Numbers, booleans, arrays and variables go in curly braces (price={15}), because quotes turn a number into text: "15" + 10 becomes 1510. A default such as { shipping = 10 } is only used when the shipping prop isn't sent. Props are read-only: the child doesn't change them, the parent sends new values.

⚡ Send it as an attribute

Write:

function Welcome({ name }) {
  return <h2>Hello, {name}!</h2>
}

function App() {
  return (
    <div>
      <Welcome name="Andi" />
      <Welcome name="Sari" />
    </div>
  )
}

Meaning: Data that a parent component sends to a child component is called props. Here App is the parent (the component that uses Welcome), and App sends name like an HTML attribute. React gathers the attributes into one props object, and { name } in the parameter takes name out of that object. So one component gives two greetings.

⚡ Quotes or curly braces

Write:

<ProductCard title="Mouse" price={15} />
Guess first: the ProductCard card shows {price + shipping}, with shipping at 10. Written as price="15", what's the result?

1510. Quotes always send a string, so + joins text. Send numbers as price={15}.

Meaning: Text goes in quotes. Numbers, booleans, arrays and variables go in curly braces.

⚡ Default values

Meaning: { shipping = 10 } in the parameter of ProductCard is a default value: it's only used when the shipping prop isn't sent. shipping={0} is still sent, so the default isn't used.

⚡ Props are read-only

Guess first: function Profile({ name }) { name = "Budi"; ... } doesn't throw. What's wrong?

The name the parent sent is lost: every Profile shows Budi. There is no error because name is only a copy of the prop, but that copy is overwritten before it is shown.

Meaning: Props belong to the parent, and the child only reads them. If a value has to change, the parent sends a new one.

What you will learn