Today: props
Today you'll make each card different with props: values a parent passes to a child component, the way you pass arguments to a function.
Where did index.html go?
From today, labs show only the files under src/. The lab supplies the same
index.html you read on Day 1, with <div id="root"></div> and a script tag
for src/main.jsx. Download project adds that file for you, so downloads
still run with Vite.
Passing props
Props look like HTML attributes. Here App gives each DayCard a day, a
topic and a number of minutes:
<DayCard day="Monday" topic="Components" minutes={30} />React collects them into one object and passes it to DayCard as its first
argument:
{ day: "Monday", topic: "Components", minutes: 30 }DayCard takes the pieces it needs out of that object with destructuring,
function DayCard({ day, topic, minutes }), and uses them in its JSX like any
other variable:
export default function DayCard({ day, topic, minutes }) {
return (
<section className="card">
<h2>{day}: {topic}</h2>
<p className="muted">{minutes} minutes, plus a 10-minute review: {minutes + 10} minutes</p>
</section>
);
}Read both files before running. Predict the three lines that start with a
number of minutes. Look closely at how each minutes is written in App.jsx.
npm install and npm run dev. Check your prediction
30 minutes, plus a 10-minute review: 40 minutes
45 minutes, plus a 10-minute review: 55 minutes
40 minutes, plus a 10-minute review: 4010 minutesWednesday's card is wrong. minutes="40" passes the string "40", and
"40" + 10 joins text, giving "4010". Quotes pass a string. Curly braces pass
any JavaScript value, so minutes={40} passes the number 40. Fix Wednesday's
card and run again.
Strings in quotes, everything else in braces
| You want to pass | Write |
|---|---|
| Text | title="Props" |
| A number | minutes={45} |
true or false | done={true} |
| A variable or expression | topic={nextTopic}, total={a + b} |
| An array or object | tags={["react", "jsx"]} |
Writing just the name, as in <Task done />, passes true. It's a common
shorthand, but done={true} is clearer while you're learning.
Props flow one way
A parent passes props down to a child. The child reads them but never changes
them. If DayCard wanted to change minutes, it couldn't: the value belongs to
App. On Day 3 you'll meet state, which is data a component owns and can
change.
What happens when a parent leaves a prop out? Delete topic="Props" from
Tuesday's card and run. The heading shows Tuesday: with nothing after it.
A missing prop is undefined, and React shows nothing for undefined. There's
no error, so check the props first when part of a component is mysteriously
blank.
Your turn
Add a level to each card
Give DayCard a fourth prop, level, with the text Easy, Medium or
Hard. Show it on its own line under the minutes, as Level: Easy. Pass a
different level to each of the three cards.
npm install and npm run dev. Hint 1: the idea
The parent passes the prop in App.jsx, and the child receives it in
DayCard.jsx. A prop only reaches the screen if both files mention it.
Hint 2: a first step
Add level to the destructuring, { day, topic, minutes, level }, then use
{level} inside a new <p>.
Show one solution
In App.jsx:
<DayCard day="Monday" topic="Components" minutes={30} level="Easy" />
<DayCard day="Tuesday" topic="Props" minutes={45} level="Medium" />
<DayCard day="Wednesday" topic="State" minutes={40} level="Hard" />In DayCard.jsx:
export default function DayCard({ day, topic, minutes, level }) {
return (
<section className="card">
<h2>{day}: {topic}</h2>
<p className="muted">{minutes} minutes, plus a 10-minute review: {minutes + 10} minutes</p>
<p>Level: {level}</p>
</section>
);
}level is text, so quotes are right here. Wednesday's minutes is now in
braces.
Completion is saved in this browser only. It does not measure mastery.