Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 02 / Topic 2 of 7

Destructuring props, and default values for the ones callers skip

This part's running example is a product card for a shop like Meera General Store, reused three different ways. examples/01_product_card.jsx destructures five props, and gives two of them a default:

// examples/01_product_card.jsx
export function ProductCard({
  name,
  price,
  imageAlt,
  inStock = true,
  discountPercent = 0,
}) {
  const hasDiscount = discountPercent > 0;
  const discountedPrice = hasDiscount
    ? Math.round(price - (price * discountPercent) / 100)
    : price;

  return (
    <article className="product-card">
      <img src="placeholder.jpg" alt={imageAlt} />
      <h3>{name}</h3>
      {hasDiscount ? (
        <p>
          <span className="original-price">{`₹${price.toLocaleString("en-IN")}`}</span>{" "}
          <span className="discounted-price">{`₹${discountedPrice.toLocaleString("en-IN")}`}</span>{" "}
          <span className="discount-badge">{`${discountPercent}% off`}</span>
        </p>
      ) : (
        <p>{`₹${price.toLocaleString("en-IN")}`}</p>
      )}
      <button disabled={!inStock}>{inStock ? "Add to cart" : "Out of stock"}</button>
    </article>
  );
}

= true and = 0 are plain JavaScript default parameter values (the same feature destructuring already gives you in any function) - they only kick in when a prop is undefined, i.e. the caller left it out entirely. name, price and imageAlt have no default: they are required by convention. Nothing in plain JavaScript enforces that - this course defers TypeScript (see the root DESIGN-NOTES.md), so a missing required prop fails at the point it is used (price.toLocaleString on undefined throws Cannot read properties of undefined), not at the point it is passed. Section 7 reuses this exact component three times to show both paths - default and explicit - actually render correctly.

🔍 Going deeper: some teams write ProductCard.propTypes = { price: PropTypes.number.isRequired } using the prop-types package to get a console warning for a missing required prop, without adopting TypeScript. This course does not install prop-types - it is an older, pre-TypeScript pattern you may still see in a codebase - but the idea ("name your required props") is worth keeping even without the package.

Done here? The practice set for Part 2 — including the solutions — is on the part page.

Search the guide