Props

Props (short for properties) are how you pass data into a component, just like arguments to a function. They make components reusable: one ProductCard, a thousand products.

Passing and reading props

  • Pass strings with quotes (name="Mouse") and everything else in braces (price={25}, inStock={true}).
  • A prop with no value means true (inStock).
  • The component receives one object, props, which we usually destructure: function ProductCard({ name, price }).

Props are read-only

A component must never modify its props. Props flow one way: parent → child. If something needs to change over time, that's state (next lesson).

jsx
function Bad({ count }) {
  count = count + 1;   // ✗ never do this
}

Default values

The children prop

Whatever you put between a component's opening and closing tags arrives as children. This is how you build layout components, cards, modals and wrappers:

Passing objects and functions

Props can be any value: objects, arrays, even functions (for callbacks, as you'll see in the Events lesson):

Typing props with TypeScript

In real projects you'll write components in .tsx files and type your props. You'll get autocomplete, and errors for missing or wrong props:

(This editor strips the types so it can render instantly. Your editor and tsc do the actual type-checking. See the TypeScript track.)

Practice

Create a PricingCard component with props plan, price, features (an array of strings, displayed with .join(', ') for now) and highlighted (a boolean that adds a coloured border). Render three plans: Free, Pro and Team.

React · live preview
Preview
✎ Check your understanding

1.Can a component change the props it receives?

2.How does a component receive content written between its tags, like <Card>Hi</Card>?