AI Hints
Home›React Tutorial›Styling and Practice
React · Lesson 17 of 120

Practice: A Product Card

Props together.

⏱ 2 min read▶ Video at 12:38🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Let's practise everything so far. A product grid, built from data, with cards that show a badge, a price, and stock, all from props.

  • Data
  • Cards from props
  • Badges and stock

A product grid

Products is our data. The grid maps each product to a card, with a key. Each card shows its name and price, a sale badge when discounted, and a stock message. Run it.

App.jsxjsx
const products = [
  { id: 1, name: "Trail 500", price: 849.5, stock: 12, sale: true },
  { id: 2, name: "Helmet", price: 40, stock: 0 },
  { id: 3, name: "Lights", price: 25, stock: 30 },
];

function Card({ name, price, stock, sale }) {
  return (
    <div className="box" style={{ width: 160 }}>
      <b>{name}</b> {sale && <span style={{ color: "crimson" }}>SALE</span>}
      <p>£{price.toFixed(2)}</p>
      <small style={{ color: stock ? "green" : "crimson" }}>{stock ? `${stock} in stock` : "Sold out"}</small>
    </div>
  );
}

export default function App() {
  return <div style={{ display: "flex", gap: 10 }}>{products.map(p => <Card key={p.id} {...p} />)}</div>;
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}

Summary

Data, map, keys, props, and conditions together make a product grid. Next, the rendering quiz.

Key takeaways
  • Data
  • map + key
  • Props
Next up: Rendering Quiz — test yourself