Chapter 22: Vue Props

Vue Props, one of the most fundamental and frequently used concepts in Vue.js.

Think of props as the official way a parent component sends data down to a child component.

It’s a one-way data flow:

  • Parent → gives data to child via props
  • Child → can read and use that data, but cannot mutate it directly (in strict mode / good practice)

This rule keeps your app predictable and much easier to debug — especially when the app grows to dozens or hundreds of components.

Core Rules of Props in Vue 3 (2026 style)

  1. Props are read-only in the child (Vue warns / throws in development if you try to mutate them)
  2. Props are passed from parent → child using attributes
  3. Child declares which props it expects using defineProps
  4. Props can be typed (especially with TypeScript — strongly recommended in 2026)
  5. Props can have default values, required flags, validators

Modern Way: <script setup> + defineProps (what you should use)

There are two syntaxes — but we will focus on the clean, modern one.

Example 1 – Very Basic Props (String & Number)

Child: GreetingCard.vue

vue

Parent: using the component

vue

What happened here?

  • :age=”28″ → the : means bind dynamically (v-bind shorthand)
  • Without : → it would be passed as string: age=”28″ → child gets “28” (string)
  • is-premium → kebab-case in HTML → automatically camelCased to isPremium in JS

Example 2 – Props with Defaults & Required

vue

Usage:

vue

Example 3 – Passing Objects & Arrays as Props

vue

Parent:

vue

Important rule (2026 best practice):

  • When passing objects/arrays → prefer reactive objects from Pinia / composables
  • Avoid mutating props inside child — if child needs to change data → emit event → parent updates

Props Summary Table (2026 Quick Reference)

Feature Syntax (defineProps) Example / Note
Basic prop name: string name=”Rahul” or :age=”28″
Optional prop isPremium?: boolean Can be omitted
Default value count?: number + ?? 0 in computed Very common pattern
Required prop title: string (no ?) Vue warns in dev if missing
Union / Literal types level?: ‘info’ | ‘success’ | ‘error’ Great with TypeScript
Complex object user: { id: number; name: string; … } Type safety + autocompletion
Validation (advanced) defineProps + custom validator function Rare — mostly use TypeScript

Pro Tips from Real 2026 Projects

  • Always use TypeScript with props → catches bugs early
  • Keep props flat & simple when possible (avoid very deep nested objects)
  • Never mutate props directly → emit ‘update:xxx’ for v-model support
  • Use kebab-case in templates (user-name) → auto-converted to camelCase (userName) in JS
  • For very large prop lists → consider slots or provide/inject / composables instead

Practice challenge:

  1. Create ProductCard.vue with props: name, price, image, isNew?, rating: number
  2. Add conditional “New!” badge, star rating display, formatted price (₹)
  3. Use it 3 times in a parent component with different data

Any part still confusing? Want me to show:

  • v-model support on custom component (update:prop pattern)?
  • Prop validation with custom functions?
  • Passing functions as props?
  • Props vs slots vs provide/inject?

Just tell me — we’ll build the next example together 🚀

Happy propping from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *