React / 16
Props: passing data into components
Props are a component's inputs. The parent writes them like HTML attributes, and the child receives them as one plain object.
Pass them like attributes
function App() {
return (
<div className="list">
<RestaurantCard name="Tasty Bites" cuisine="South Indian" rating={4.5} />
<RestaurantCard name="Green Bowl" cuisine="Salads" rating={4.1} />
</div>
);
}- Strings go in quotes:
name="Tasty Bites". - Everything else goes in braces: numbers
{4.5}, booleans{true}, arrays, objects, functions, variables. - A bare attribute means
true:<RestaurantCard isOpen />is the same asisOpen={true}.
Receive them as one object
React collects all the attributes into a single object called props and passes it to your component as the first argument.
function RestaurantCard(props) {
return (
<div className="card">
<h3>{props.name}</h3>
<p>{props.cuisine} · ★ {props.rating}</p>
</div>
);
}parent · passes
<RestaurantCard name="Tasty Bites" rating={4.5} />props · one plain object
{ name: "Tasty Bites", rating: 4.5 }child · receives
function RestaurantCard({ name, rating }) { … }It's the same props object you passed as the second argument of createElement in Chapter 1. JSX just builds it for you.
Destructure in the parameter list
Typing props. everywhere gets old. Pull the values out right in the parameter list, which is called destructuring:
function RestaurantCard({ name, cuisine, rating }) {
return (
<div className="card">
<h3>{name}</h3>
<p>{cuisine} · ★ {rating}</p>
</div>
);
}Same component, easier to read, and the first line now lists exactly which props it expects.
Default values
Give a prop a fallback with = inside the destructuring:
function RestaurantCard({ name, cuisine = "Various", rating = 0 }) {
// <RestaurantCard name="New Place" /> → cuisine is "Various", rating is 0
}The default is used only when the prop is missing or undefined.
Passing a whole object
When the data is already an object, you have two options:
const restaurant = { name: "Tasty Bites", cuisine: "South Indian", rating: 4.5 };
// Option 1: one prop holding the object. The child reads data.name
const one = <RestaurantCard data={restaurant} />;
// Option 2: spread it. Same as name="…" cuisine="…" rating={4.5}
const two = <RestaurantCard {...restaurant} />;Both are fine. Spreading is short, but it also passes along any extra fields the object happens to have, so be explicit when it matters.
Why style has double braces
The style prop takes an object, not a CSS string. The outer braces say "JavaScript here"; the inner braces are the object.
<div style={{ backgroundColor: "#f4ece0", padding: 12 }}>Tasty Bites</div>
// exactly the same thing:
const cardStyle = { backgroundColor: "#f4ece0", padding: 12 };
<div style={cardStyle}>Tasty Bites</div>CSS property names are camelCase (backgroundColor, not background-color), and plain numbers become pixels for sizes like padding. Prefer className and a stylesheet for most styling; inline style shines for values that change at runtime, like style={{ width: progress + "%" }}.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
