Props Advanced

Back

Loading concept...

๐ŸŽ Vue.js Props: The Gift Box Story

The Magic of Unwrapping Gifts

Imagine you receive a gift box ๐ŸŽ from your friend. Inside the box, thereโ€™s a toy car, a puzzle, and some candy. In Vue.js, this gift box is called props โ€“ things a parent component gives to a child component.

Now, hereโ€™s the exciting part: How do you take things out of the box?


๐Ÿ“ฆ What Are Props Destructuring Caveats?

The Story

Think of props like a magic gift box that keeps changing. If you take the toy out and put it on your shelf, your toy on the shelf wonโ€™t magically change when someone puts a NEW toy in the gift box!

<!-- โŒ The Problem: Breaking the Magic Connection -->
<script setup>
const { count } = defineProps(['count'])

// count is just a NUMBER now (like 5)
// It's NOT connected to the gift box anymore!
// If the box updates, your shelf doesn't know
</script>

What Happens Here?

  1. You open the gift box (defineProps)
  2. You take count out and put it on your shelf
  3. The gift box gets a new count? Your shelf still has the old one!

๐Ÿ”ฎ The Magic Solution: Reactive Props Destructure

Vue 3.5+ Gave Us Magic Powers!

Starting from Vue 3.5, when you destructure props in <script setup>, the magic connection stays alive!

<!-- โœ… Vue 3.5+: The Magic Connection Stays! -->
<script setup>
const { count } = defineProps(['count'])

// Now count IS reactive!
// When gift box updates, your shelf updates too!
</script>
graph TD A["๐ŸŽ Parent Component"] -->|Sends Props| B["๐Ÿ“ฆ defineProps"] B -->|Vue 3.5+ Magic| C["โœจ Reactive count"] C -->|Auto Updates| D["๐Ÿ–ฅ๏ธ Your Template"] style A fill:#4CAF50,color:#fff style B fill:#2196F3,color:#fff style C fill:#FF9800,color:#fff style D fill:#9C27B0,color:#fff

โš ๏ธ The Caveats (Watch Out!)

Caveat 1: Default Values Need Special Care

<script setup>
// โŒ This default won't be reactive
const { count = 0 } = defineProps(['count'])

// โœ… Use withDefaults for reactive defaults
const props = withDefaults(
  defineProps<{ count?: number }>(),
  { count: 0 }
)
</script>

Why? Default values are like putting a placeholder toy on your shelf. The placeholder isnโ€™t connected to the gift box!

Caveat 2: Passing to Functions

<script setup>
const { count } = defineProps(['count'])

// โš ๏ธ Be careful when passing to functions!
watch(
  // โŒ This loses reactivity
  count,
  (newVal) => console.log(newVal)
)

watch(
  // โœ… Use a getter to keep reactivity
  () => count,
  (newVal) => console.log(newVal)
)
</script>

๐ŸŽจ Real Examples

Example 1: A Counter Display

<!-- ParentComponent.vue -->
<template>
  <ChildDisplay :score="playerScore" />
</template>

<script setup>
import { ref } from 'vue'
const playerScore = ref(100)
</script>
<!-- ChildDisplay.vue -->
<script setup>
const { score } = defineProps(['score'])
// score updates when parent changes it! โœจ
</script>

<template>
  <div class="score-box">
    ๐Ÿ† Score: {{ score }}
  </div>
</template>

Example 2: User Profile Card

<!-- ProfileCard.vue -->
<script setup>
const {
  name,
  avatar,
  isOnline
} = defineProps(['name', 'avatar', 'isOnline'])
</script>

<template>
  <div class="card">
    <img :src="avatar" />
    <h3>{{ name }}</h3>
    <span :class="isOnline ? 'green' : 'gray'">
      {{ isOnline ? '๐ŸŸข Online' : 'โšซ Offline' }}
    </span>
  </div>
</template>

๐Ÿ“Š Quick Comparison Table

Situation Vue < 3.5 Vue 3.5+
const { x } = defineProps() โŒ Not reactive โœ… Reactive
Default values โŒ Problem โš ๏ธ Use withDefaults
In watch() Use getter () => x Use getter () => x
In template โœ… Works โœ… Works

๐Ÿง  Remember This!

The Gift Box Rule ๐ŸŽ

When you destructure props in Vue 3.5+, the magic connection stays alive. But always use getters in functions like watch() to keep the connection working!

Simple Mental Model

๐Ÿ“ฆ Props = Gift Box (always changing)
๐Ÿ“‹ Destructured = Items on your shelf
๐Ÿ”ฎ Vue 3.5+ = Magic shelf that updates automatically!
โš ๏ธ Functions = Need special getter wrapper

๐ŸŽฏ Key Takeaways

  1. Props Destructuring = Taking items out of the gift box
  2. Vue 3.5+ = The magic connection stays alive
  3. Watch out for default values (use withDefaults)
  4. In functions like watch(), wrap with a getter: () => propName
  5. In templates, destructured props always work!

๐Ÿ’ก Pro Tips

Tip 1: Always Use TypeScript for Clarity

<script setup lang="ts">
interface Props {
  count: number
  message?: string
}

const { count, message = 'Hello' } = defineProps<Props>()
</script>

Tip 2: Computed for Transformations

<script setup>
import { computed } from 'vue'
const { price } = defineProps(['price'])

// โœ… Perfect for derived values
const formattedPrice = computed(() => `${price.toFixed(2)}`)
</script>

You did it! ๐ŸŽ‰ Now you understand how props destructuring works in Vue.js. Remember: the gift box is always connected to your shelf in Vue 3.5+, but be careful when passing values to functions!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.