AI Hints
Home›React Tutorial›Components and Props
React · Lesson 8 of 120

Props

Passing data in.

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

A component is more useful when it can show different data. Props are how you pass data in. They look like HTML attributes.

  • Different data
  • Props
  • Like attributes

A product card with props

Product card receives props, an object holding everything passed in. App uses it three times, with a different model and price each time. Run it. One component, three cards.

App.jsxjsx
function ProductCard(props) {
  return (
    <div className="box">
      <b>{props.model}</b> — £{props.price}
    </div>
  );
}

export default function App() {
  return (
    <div>
      <ProductCard model="Trail 500" price={849.5} />
      <ProductCard model="City 200" price={450} />
      <ProductCard model="Volt 7" price={2199} />
    </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}

Passing values

Text can go in quotes. Anything else, numbers, lists, objects, or functions, goes in curly braces. Props flow one way, from parent to child.

keyvalue
Textmodel="Trail 500"
Numbersprice={849.5}
Anything else{...}
Directionparent to child

Summary

Props pass data into a component, so one component can show many things. Next, destructuring props.

Key takeaways
  • props.model
  • price={849.5}
  • Parent to child
Next up: Destructuring Props — cleaner components