Thuta Learning
React
BasicWeb DevelopmentIntermediate

Props

ReactLesson 625 min

What you'll walk away with

  • Turn a component into a reusable template driven by props
  • Pass string props in quotes and number, boolean, object and array props in braces
  • Give default values in destructuring to handle a missing prop
  • Write a wrapper component with the children prop
  • Explain why props must never be modified

Explain it simply

Explain it simply

Props are the settings you hand to a component. Think of an order form at a coffee shop: the form is always the same, but filling in different values, such as size large and oat milk, produces a different coffee. In <CourseCard title="React" lessons={18} />, title and lessons are props.

They are written like HTML attributes, but a prop can be any JavaScript value: a string, a number, an array, an object, even a function. The component receives those values as an argument and shows them in its UI.

Problem → Why → Solution

The problem: a component that can only show fixed text

The WelcomeCard from the components lesson is reusable, but every card says "React Course". To show React, Node.js and CSS cards you end up copying the same markup into ReactCard, NodeCard and CssCard, and the benefit of extracting a component is gone.

Cards.jsx
function ReactCard() {
  return <article className="card"><h2>React</h2><p>18 lessons</p></article>;
}

function NodeCard() {
  return <article className="card"><h2>Node.js</h2><p>12 lessons</p></article>;
}

// ...one copy per course: the markup is duplicated again

Why does this happen?

The card's shape (markup) and its data (title, lessons) are fused together, so new data demands a new component. Props separate the two: the shape lives once inside CourseCard, and the parent sends different data for each card, exactly like calling one function with different arguments.

Core concept: the props object and one-way data flow

When you write <CourseCard title="React Foundation" lessons={18} />, React collects every attribute into one object, { title: "React Foundation", lessons: 18 }, and passes it as the first argument of the CourseCard function. That object is props. You can accept it as function CourseCard(props) and read props.title, but destructuring right in the parameter, function CourseCard({ title, lessons }), is more common because the signature then shows which props the component needs.

If the parent does not pass a prop, its value is undefined. A default in the destructuring, such as { level = "Beginner" }, is used whenever the value is undefined. Note that defaults apply only to undefined: passing null or 0 does not trigger the default. For boolean props, writing <Badge isNew /> with no value is the same as isNew={true}. To pass every property of an object as props, use spread syntax: <CourseCard {...course} />.

Whatever you put between a component's opening and closing tags is passed as a special prop called children. In <Panel title="Price"><p>45,000 MMK</p></Panel>, the <p> is Panel's children. This is the main way to build frame components such as cards, modals and layouts: the frame does not need to know what goes inside; it just renders {children} where the content belongs.

Props flow one way, from parent to child. A child may read its props but never change them. In development React freezes the props object, so an assignment like props.title = ... throws a TypeError. To change data, the parent that owns it updates its state and passes new props down, which you will start doing in the state lesson.

Mental model: props are function arguments
  1. Parent writes <Child a="x" b={1} />
  2. React collects them into { a, b }
  3. Child destructures and reads them
  4. New data means the parent re-sends

Props flow downhill like water; a child cannot push changes back up through them.

General pattern

jsx
function Child({ textProp, numberProp, optionalProp = "default", children }) {
  return <element>{textProp} {numberProp} {optionalProp} {children}</element>;
}

<Child textProp="a string" numberProp={42}>
  nested content becomes children
</Child>

The child destructures its props in the parameter list, with = for defaults. The parent passes strings in quotes and everything else in braces, and content between the tags becomes children.

Example: a CourseCard driven by props

CourseCard holds the template and App supplies the data. Notice that lessons={18} passes a number in braces, while title and level are strings in quotes. To show a second course, App only needs another <CourseCard /> with different values; the card's markup stays untouched.

jsx
function CourseCard({ title, level, lessons }) {
  return (
    <article className="course-card">
      <h2>{title}</h2>
      <p>Level: {level}</p>
      <p>{lessons} lessons included</p>
    </article>
  );
}

function App() {
  return (
    <CourseCard
      title="React Foundation"
      level="Starter"
      lessons={18}
    />
  );
}

Code breakdown

  1. Line 1function CourseCard({ title, level, lessons }) {

    The props object is destructured right in the parameter. Without the braces, function CourseCard(title) would make title the entire props object, and rendering it would throw an error.

  2. Line 4<h2>{title}</h2>

    Whatever title the parent passes appears here. CourseCard itself has no idea which course it shows; it only displays what it receives.

  3. Line 6<p>{lessons} lessons included</p>

    lessons is the number 18, so this shows "18 lessons included". Arithmetic such as lessons + 1 would correctly give 19.

  4. Lines 14-16title="React Foundation"

    On the parent side, props are written like attributes. With several props, one prop per line, as here, is easier to read.

  5. Line 16lessons={18}

    Braces make it a number. Written as lessons="18" it would be a string, and lessons + 1 would produce "181".

You should see
A card will appear showing the course title, level, and lesson count.

Combining children and default values

Here Panel is a frame component that renders its children. PriceTag gives defaults for currency and discount, so the Mouse card works with discount treated as 0 even though none was passed. discount > 0 && ... shows the badge only when there is a discount.

Shop.jsx
function Panel({ title, children }) {
  return (
    <section className="panel">
      <h3>{title}</h3>
      {children}
    </section>
  );
}

function PriceTag({ amount, currency = "MMK", discount = 0 }) {
  const finalAmount = amount - (amount * discount) / 100;
  return (
    <p className="price">
      {finalAmount.toLocaleString("en-US")} {currency}
      {discount > 0 && <span className="badge"> -{discount}%</span>}
    </p>
  );
}

export default function Shop() {
  return (
    <main>
      <Panel title="Keyboard"><PriceTag amount={45000} discount={10} /></Panel>
      <Panel title="Mouse"><PriceTag amount={18000} /></Panel>
    </main>
  );
}

The rendered result shows "40,500 MMK -10%" in the Keyboard panel and "18,000 MMK" with no badge in the Mouse panel.

In real projects

In real projects

In real apps data usually arrives from an API as an array, and each item is passed as props to draw a card, as in {courses.map((course) => <CourseCard key={course.id} {...course} />)}. A design-system Button produces many looks from props such as variant="primary", size="small" and disabled, while components like Layout, Modal and Card accept their inner content through children.

Functions are passed as props too: <SearchBox onSearch={handleSearch} /> lets the child tell the parent what the user typed. That does not break one-way data flow, because the child never edits the parent's data directly; it calls a function the parent provided. The events lesson covers this pattern in depth. In TypeScript projects props get types, so the editor flags a missing required prop immediately.

Common mistakes

Common mistakes

1. Passing a number in quotes

With <CourseCard lessons="18" />, lessons is a string, so {lessons + 1} shows "181" instead of 19. No error appears, so it often goes unnoticed. Pass every non-string value in braces: lessons={18}.

2. Forgetting the destructuring braces

Writing function CourseCard(title) makes title the whole props object, not a string. Then <h2>{title}</h2> throws "Objects are not valid as a React child (found: object with keys {title})". Add the braces: function CourseCard({ title }).

3. Modifying props inside the child

props.title = props.title.toUpperCase(); throws the TypeError "Cannot assign to read only property 'title'" in development. If you need a transformed value, store it in a new variable: const upperTitle = title.toUpperCase();.

4. Expecting a default to cover null

{ level = "Beginner" } does nothing when the parent passes level={null}: null is not undefined, so level stays null and nothing shows. If API data may contain null, use level ?? "Beginner".

Best practices

Best practices

  • Destructure props in the parameter list so the signature shows exactly what data the component needs.
  • Name props from the component's point of view: title beats reactCourseTitle for a CourseCard.
  • Start boolean props with is or has, such as isOnline or hasDiscount, so they read like yes-or-no questions.
  • If a component collects ten or more props, it is doing too much; consider children or splitting it up.

Mini challenge

Mini challenge: a reusable profile card

Write a ProfileCard component and use it three times inside a Team component, passing different props for each member.

  • ProfileCard destructures name, role, years (a number) and isOnline (a boolean)
  • role defaults to "Member"
  • Show an "online" badge with && when isOnline is true
  • Render children at the bottom of the card, for example a short bio
Team.jsx
function ProfileCard(/* TODO: destructure props, default role = "Member" */) {
  return (
    <article className="profile">
      {/* TODO: name in an <h3> */}
      {/* TODO: "<role> - <years> years" */}
      {/* TODO: online badge only when isOnline */}
      {/* TODO: children */}
    </article>
  );
}

export default function Team() {
  return (
    <section>
      {/* TODO: three ProfileCards; one without role, one with isOnline */}
    </section>
  );
}

Expected: Three cards appear: "Aye Aye" (Designer - 4 years, online badge and a bio), "Min Thu" (Developer - 2 years) and "Hla Hla" (Member - 1 years, because no role was passed).

Show hint

A boolean prop can be written without a value: <ProfileCard isOnline />. To pass children, use opening and closing tags, <ProfileCard ...><p>...</p></ProfileCard>, instead of a self-closing tag.

Show solution
Team.jsx
function ProfileCard({ name, role = "Member", years, isOnline, children }) {
  return (
    <article className="profile">
      <h3>{name}</h3>
      <p>
        {role} - {years} years
      </p>
      {isOnline && <span className="badge">online</span>}
      {children}
    </article>
  );
}

export default function Team() {
  return (
    <section>
      <ProfileCard name="Aye Aye" role="Designer" years={4} isOnline>
        <p>Turns sketches into clean interfaces.</p>
      </ProfileCard>
      <ProfileCard name="Min Thu" role="Developer" years={2} />
      <ProfileCard name="Hla Hla" years={1} />
    </section>
  );
}

Quick quiz 1 / 3

Given function Info({ count }) { return <p>{count + 1}</p>; }, what does <Info count="5" /> show?

Quick quiz 2 / 3

In <Card><p>Hello</p></Card>, which prop gives Card the <p>Hello</p>?

Quick quiz 3 / 3

A child wants to change the title it received from its parent. What is the React way?

What to remember

  • Props are data from parent to child; React passes all attributes as one object.
  • Pass strings in quotes and other values in braces, and set defaults in the destructuring.
  • Content between the tags arrives as the children prop, perfect for frame components.
  • Props are read-only, and data flows one way, from parent to child.

React Documentation — React

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Props | Thuta Learning