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> |
Appis the parent:AppusesWelcomeand sends it the data.Welcomeis the child.- React gathers every attribute on the
<Welcome ... />tag into one object calledprops, and hands it to theWelcomefunction as its first parameter. { name }in the parameter is destructuring: a way to take thenameproperty straight out of the props object. It's the same as writingfunction Welcome(props)and then usingprops.name.{name}inside the<h2>shows the value ofname. So one component gives two different greetings.
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 |
- There are three tags, so there are three props objects and three cards. It's still one component.
shipping = 10is a default value: it's used when theshippingprop isn't sent. Laptop and Mouse don't send it, so their shipping is 10.- Headset sends
shipping={0}, so the default isn't used. A default only replaces a prop that wasn't sent (its value isundefined), not a prop whose value is0. className="price"in JSX becomesclass="price"on the page.
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?
title="Laptop": quotes send a string (text).price={750}: curly braces send the JavaScript value as it is, here a number.- Quotes always give a string,
price="15"included. On the screen,{price}looks the same for a string and a number. But in JavaScript"15" + 10joins the text, giving1510. So that Mouse card shows1510, not25.
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>
}
- In development mode (when the app runs with
npm run dev), React freezes the props object so it can't be changed. So the lineprops.title = "Promo"throws aTypeErrorright away (in Chrome:Cannot assign to read only property 'title' of object). - Overwriting a destructured variable, such as
name = "Budi"insideWelcome, doesn't throw, becausenameis only a copy. But the name the parent sent is lost: every greeting shows Budi. Treat props as read-only, always.
⚠️ Props that arrive wrong, or not at all
- Sending a number in quotes.
price="15"is a string, so+joins text. Send numbers asprice={15}. - Forgetting to send a prop. Its value is
undefined:{price}shows nothing, andundefined + 10givesNaN(not a number). Send every prop the child needs, or give it a default. - Typing the value inside the child.
<h3>Laptop</h3>insideProductCardgives every card the title Laptop. Show the prop instead:<h3>{title}</h3>. The second exercise checks that the content of<h3>and of the.priceelement starts with curly braces. - Different prop names in parent and child.
Appsendsname, the child readsuserName: you getundefined. The names must match exactly, capital letters included.
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.