Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Components and JSX  ›  Lesson

Props — Data Flowing Down

Props vs State 14 minProps are the read-only inputs a parent passes to a child component, letting one component show different data each time it is used.

Props — Data Flowing Down

Our BookCard from earlier always shows The Hobbit. A card that can only ever show one book is not much of a component. Props are how a parent says, "this time, show this."

The rule

Props flow down, arrive as one object, and are read-only.

Flow down. A parent passes props to a child by writing attributes on the child's tag:

export default function App() {
  return (
    <main>
      <BookCard title="The Hobbit" author="J. R. R. Tolkien" pages={310} />
      <BookCard title="Dune" author="Frank Herbert" pages={412} />
    </main>
  );
}

Strings can use quotes. Anything else — numbers, booleans, arrays, objects, functions — goes in braces: pages={310}, read={true}. Writing just read is shorthand for read={true}.

One object. The child receives every prop in a single object, its first parameter. Destructuring pulls out the ones you need, and you can give defaults:

function BookCard({ title, author, pages = 0 }) {
  return (
    <article className="book">
      <h2>{title}</h2>
      <p>{author} · {pages} pages</p>
    </article>
  );
}

Read-only. A component must never change its own props. title = title.toUpperCase() inside BookCard breaks the rule. Props belong to the parent; the child only reads them. If data needs to change over time, that is a job for state, which you meet in the State section. In one line: props are handed to you; state is kept by you.

Props can carry anything, including functions. <BookCard onRemove={handleRemove} /> gives the child a function it can call to tell the parent something happened — the only way information travels back up. And when the parent re-renders with different props, the child simply re-renders with the new values; nothing needs to be copied or synced.

The shortcut

Think of props as function arguments with names. <BookCard title="Dune" pages={412} /> means roughly "call BookCard with { title: 'Dune', pages: 412 }."

Following one prop from parent to screenStep 1 of 4
  1. Parent writes it

    App renders <BookCard title="Dune" pages={412} />.

Typical slips the shortcut catches: forgetting the braces in the parameter (function BookCard(title) receives the whole object, not the string), and writing pages="412" when you meant a number.

Drills

Answer fast — trust the rule.

quick drill

How does BookCard receive the props from <BookCard title="Dune" />?

quick drill

Which passes the number 3 rather than the string "3"?

quick drill

function BookCard(title) { return <h2>{title}</h2>; } is used as <BookCard title="Dune" />. What is wrong?

quick drill

Inside BookCard, which is allowed?

Remember

  • Props flow down, arrive as one object, and are read-only.
  • Treat props as named function arguments; destructure them in the parameter list.
  • Trap: missing braces around the parameter, and quotes around numbers.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne card, many books

Your BookCard always shows 'The Hobbit'. Explain how to change it so App can show three different books with it, including how the parent passes the data, how the child reads it, and what the child must not do with it.