Aral Code

Components & Props Lesson 5 of 13

React Props — Passing Data to Components

Make components reusable with props — pass strings, numbers, arrays and functions, destructure props, set default values and use the children prop.

A component that always shows the same thing isn't very useful. Props (short for properties) let you pass data into a component, the way attributes pass data to HTML tags. One ProductCard can then show any product.

Passing props

Example A reusable ProductCard
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
  body { font-family: sans-serif; }
  .grid { display: flex; gap: 10px; flex-wrap: wrap; padding: 12px; }
  .card { border: 1px solid #ddd; border-radius: 12px; padding: 12px; width: 140px; text-align: center; }
  .price { color: #0f5c4d; font-weight: bold; }
  .out { color: crimson; font-size: 13px; }
</style>
<script type="text/babel">
  function ProductCard({ name, price, emoji, stock }) {
    return (
      <div className="card">
        <div style={{ fontSize: 36 }}>{emoji}</div>
        <h4>{name}</h4>
        <p className="price">₱{price.toFixed(2)}</p>
        {stock === 0 && <p className="out">Sold out</p>}
      </div>
    );
  }

  function App() {
    return (
      <div className="grid">
        <ProductCard name="Pandesal" price={25} emoji="🥖" stock={40} />
        <ProductCard name="Kape" price={8} emoji="☕" stock={200} />
        <ProductCard name="Chips" price={15} emoji="🍟" stock={0} />
      </div>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
  • Text props use quotes: name="Pandesal".
  • Any other JavaScript value uses braces: price={25}, stock={0}.
  • The component receives all props as one object. { name, price, emoji, stock } destructures it into variables.

Props are read-only

A component must never change its own props. Props flow down from parent to child, like a teacher handing out papers. If something needs to change, it belongs in state (next lessons).

Default values

function Avatar({ name, size = 48 }) {
  return <div style={{ width: size, height: size }}>{name[0]}</div>;
}

<Avatar name="Ana" />          // size is 48
<Avatar name="Ben" size={80} /> // size is 80

Passing arrays and objects

Example Props with an object and an array
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  function ReportCard({ student, subjects }) {
    const avg = subjects.reduce((sum, s) => sum + s.grade, 0) / subjects.length;
    return (
      <div style={{ fontFamily: "sans-serif", padding: 12 }}>
        <h3>{student.name} – Grade {student.level}</h3>
        <ul>
          {subjects.map((s) => <li key={s.name}>{s.name}: {s.grade}</li>)}
        </ul>
        <strong>Average: {avg.toFixed(2)}</strong>
      </div>
    );
  }

  const data = [
    { name: "Math", grade: 88 },
    { name: "Science", grade: 92 },
    { name: "English", grade: 85 },
  ];

  ReactDOM.createRoot(document.getElementById("root")).render(
    <ReportCard student={{ name: "Juan", level: 9 }} subjects={data} />
  );
</script>

map turns each subject into an <li>. Lists get their own lesson, including what key does.

The children prop

Whatever you put between a component's opening and closing tags arrives as children. It's perfect for wrapper components:

Example A Box component with children
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  function Box({ title, color = "#0f5c4d", children }) {
    return (
      <section style={{ border: `2px solid ${color}`, borderRadius: 12, padding: 12, margin: 10, fontFamily: "sans-serif" }}>
        <h3 style={{ color, marginTop: 0 }}>{title}</h3>
        {children}
      </section>
    );
  }

  function App() {
    return (
      <>
        <Box title="Reminder">
          <p>Bring your PE uniform on Wednesday.</p>
        </Box>
        <Box title="Warning" color="crimson">
          <p>The quiz is tomorrow!</p>
          <button>Got it</button>
        </Box>
      </>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>

Passing functions as props

Props can also be functions, which lets a child tell its parent that something happened:

function DeleteButton({ onDelete }) {
  return <button onClick={onDelete}>Delete</button>;
}

<DeleteButton onDelete={() => alert("Deleted!")} />

By convention, these props start with on: onDelete, onSelect, onChange.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you pass the number 25 as a prop?
2. Can a component change its own props?
3. What is the children prop?
4. What does function Card({ title }) do?

By Sir Reyel · Last updated 2026-12-04