Skip to content

Props

The Course
•
20 min study

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:

JSX
<DayCard day="Monday" topic="Components" minutes={30} />

React collects them into one object and passes it to DayCard as its first argument:

JavaScript
{ 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:

JSX
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.

One component, three cards React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import DayCard from "./DayCard.jsx"; export default function App() { return ( <main> <h1>This week</h1> <DayCard day="Monday" topic="Components" minutes={30} /> <DayCard day="Tuesday" topic="Props" minutes={45} /> <DayCard day="Wednesday" topic="State" minutes="40" /> </main> ); } 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> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Check your prediction
Text
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 minutes

Wednesday'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 passWrite
Texttitle="Props"
A numberminutes={45}
true or falsedone={true}
A variable or expressiontopic={nextTopic}, total={a + b}
An array or objecttags={["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

Exercise

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.

Your working space React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import DayCard from "./DayCard.jsx"; export default function App() { return ( <main> <h1>This week</h1> <DayCard day="Monday" topic="Components" minutes={30} /> <DayCard day="Tuesday" topic="Props" minutes={45} /> <DayCard day="Wednesday" topic="State" minutes="40" /> </main> ); } 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> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then 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:

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:

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.

Was this article helpful?

Your response is saved on this device.