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 7 of 7

Putting it together: one ProductCard, reused three ways

Section 2 defined ProductCard. examples/01_product_card.test.jsx renders it three times with three different, realistic prop combinations, and all three really pass:

# render 1 - only the required props, everything else defaults
<ProductCard name="Atta 5kg" price={260} imageAlt="A 5kg bag of atta" />
→ "Atta 5kg", "₹260", an enabled "Add to cart" button, no discount badge

# render 2 - discountPercent passed explicitly
<ProductCard name="Toor Dal 1kg" price={140} imageAlt="A 1kg packet of toor dal" discountPercent={20} />
→ both "₹140" (the original price) and "₹112" (the discounted price) shown, alongside a "20% off" badge

# render 3 - inStock passed explicitly as false
<ProductCard name="Ghee 500ml" price={320} imageAlt="A 500ml jar of ghee" inStock={false} />
→ "₹320" with a disabled "Out of stock" button, price unaffected by stock status

Every difference between these three renders came from the props passed in - ProductCard's own code never changed. That is composition and props working together exactly as this part describes them: one function, several shapes of input, zero copy-pasted markup. Exercise 6 pushes this one step further by driving all three renders from a single array of product data instead of three hand-written JSX tags.

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

Search the guide