Learn Props in Vue

Props are data a parent sends to a child. The child declares them with defineProps({ name: String, price: Number }), then the parent sends them through…

In the components lesson, placing <StallHeader /> twice gave you two copies that were exactly alike. For menu cards, copies that are alike are not enough. The Fried Rice card and the Iced Tea card have the same shape, but different names and prices.

Props fill in that difference. Props are data a parent sends to a child through attributes on the child's tag. Picture the price-label holders in the display case at Mrs. Sri's stall. Every holder has the same shape, but Mrs. Sri slides a different slip into each one: "Fried Rice, $15", "Iced Tea, $5". The holder only shows what's on its slip. If a price has to change, Mrs. Sri writes a new slip; the holder doesn't. In Vue, the holder is the child component, the slip is the props, and Mrs. Sri is the parent.

The parent sends, the child receives

The child declares which props it takes, with their types, using defineProps:

<!-- MenuCard.vue -->
<script setup>
const props = defineProps({
  name: String,
  price: Number,
  stock: Number,
})
</script>

<template>
  <div class="card">
    <h3>{{ name }}</h3>
    <p>${{ price }}</p>
    <small>Stock: {{ stock }}</small>
  </div>
</template>

The parent fills those props in through attributes. Each <MenuCard> tag becomes one copy of the card:

<!-- App.vue -->
<script setup>
import MenuCard from "./MenuCard.vue"
</script>

<template>
  <MenuCard name="Fried Rice" :price="15" :stock="8" />
  <MenuCard name="Chicken Noodles" :price="12" :stock="0" />
  <MenuCard name="Iced Tea" :price="5" :stock="20" />
</template>
Tag in App.vue What shows
name="Fried Rice" :price="15" :stock="8" Fried Rice, $15, Stock: 8
name="Chicken Noodles" :price="12" :stock="0" Chicken Noodles, $12, Stock: 0
name="Iced Tea" :price="5" :stock="20" Iced Tea, $5, Stock: 20

The colon: text or a JavaScript value

Take a guess: MenuCard declares price: Number and shows Takeaway: ${{ price + 2 }}. If the parent writes <MenuCard name="Fried Rice" price="15" />, with no colon, what shows?

The card says Takeaway: $152. Text plus a number in JavaScript means joining, not adding: "15" + 2 becomes "152". With :price="15", it says Takeaway: $17.

In development mode, the console tells you what's wrong: [Vue warn]: Invalid prop: type check failed for prop "price". Expected Number with value 15, got String with value "15". The exercise editor uses the production build, so warnings like this don't appear there. The odd number is your only clue.

Type, required and default

Each prop can be given rules, written as an object:

const props = defineProps({
  name: { type: String, required: true },
  price: { type: Number, required: true },
  stock: { type: Number, default: 0 },
  notes: { type: Array, default: () => [] },
})

Props are read-only

Data flows one way: from parent to child. The child may read props, but never change them. Here is the wrong way, a button in the child that tries to lower the prop stock:

<!-- MenuCard.vue, the wrong way -->
<button @click="stock--">Buy 1</button>

📌 Two-word prop names

In the script, a prop name is written camelCase: promoPrice. In the parent's template, it's usually written kebab-case: <MenuCard :promo-price="13" />. Vue treats promo-price and promoPrice as the same prop, so both work.

.vue files and the exercise editor

The exercise editor runs one component: the script, the ---template--- line, then the template. In the editor, a child is written as an object in the script, and placed with <component :is>:

const MenuCard = {
  props: {
    name: String,
    price: Number,
    stock: Number,
  },
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>{{ "$" + price }}</p>
      <small>Stock: {{ stock }}</small>
    </div>
  `,
}

---template---
<div>
  <component :is="MenuCard" name="Fried Rice" :price="15" :stock="8" />
  <component :is="MenuCard" name="Iced Tea" :price="5" :stock="20" />
</div>
In a .vue file In the exercise editor
defineProps({ ... }) in MenuCard.vue the props: { ... } option in the MenuCard object
the <template> of MenuCard.vue the template option, as text between backticks
import MenuCard from "./MenuCard.vue" then <MenuCard ... /> <component :is="MenuCard" ... />

⚠️ Why is the price weird, or the button doing nothing?

TL;DR: Props are data a parent sends to a child. The child declares them with defineProps({ name: String, price: Number }), then the parent sends them through attributes on the child's tag: <MenuCard name="Fried Rice" :price="15" />. Without a colon, you send text. With a colon, you send a JavaScript value, such as a number. type, required and default set the rules for each prop, and an array or object default is written as a function. Props are read-only: data flows one way, and the parent makes the changes. In the exercise editor, a child is written as an object with the props and template options, then placed with <component :is="MenuCard">.

⚡ The child declares its props

Write:

<!-- MenuCard.vue -->
<script setup>
const props = defineProps({
  name: String,
  price: Number,
  stock: Number,
})
</script>

Meaning: Props are data from a parent to a child, sent through attributes on the child's tag. MenuCard.vue here is a child that takes three props. defineProps is a compiler macro (an instruction Vue itself recognises), so it needs no import. In the template, props are read directly, {{ name }}. In the script, through props.name.

⚡ The parent sends them through attributes

Write:

<MenuCard name="Fried Rice" :price="15" :stock="8" />
<MenuCard name="Iced Tea" :price="5" :stock="20" />
Guess first: MenuCard shows Takeaway: ${{ price + 2 }}. If the parent writes price="15" with no colon, what shows?

Takeaway: $152. Without the colon, the text "15" is sent, and in JavaScript text plus a number means joining.

Meaning: The parent places two copies of MenuCard, each with its own data. Without a colon, you send text. With a colon (:price), you send a JavaScript value, here the number 15.

⚡ Props are read-only

Guess first: the child writes props.name = "Budi". Does the name change?

No. Props are read-only (they can only be read), so the change is refused.

Meaning: Data flows one way, from parent to child. The parent is the one that changes the data. If the child needs to tell the parent something, it sends an event, which is the topic of the next lesson, Emits.

What you will learn