Javascript is required
·
1 min read
·
1454 views

Vue Tip: Destructure Props in Composition API Without Losing Reactivity

Vue Tip: Destructure Props in Composition API Without Losing Reactivity Image

Inside <script setup> we use the defineProps() compiler macro to access props:

DoubleCount.vue
1<script setup lang="ts">
2const props = defineProps<{ count: number }>()
3</script>

Info

defineProps is a compiler macro and does not need to be imported.

As we all love destructuring objects in JavaScript you might want to try to destructure the props object:

DoubleCount.vue
1<script setup lang="ts">
2const { count } = defineProps<{ count: number }>()
3</script>

Danger

When you destructure the props object the reactivity is lost.

By destructuring the props object, the count variable becomes a primitive value (in our case of type number) and is no longer a ref or reactive object.

You can try it yourself in the following StackBlitz demo. Click the "Increment" button multiple times and you will see that the double count value is not updated and always shows 0:

The simplest solution is to access props as props.count in order to retain reactivity:

DoubleCount.vue
1<script setup lang="ts">
2import { computed } from 'vue'
3
4const props = defineProps<{ count: number }>()
5const doubleCount = computed(() => props.count * 2)
6</script>
7
8<template>Double Count: {{ doubleCount }}</template>

Try it yourself, now the double count is correctly increased if the "Increment" button is clicked:

If you cannot live without destructuring Vue provides a special helper toRefs:

DoubleCount.vue
1<script setup lang="ts">
2import { computed, toRefs } from 'vue'
3
4const props = defineProps<{ count: number }>()
5
6const { count } = toRefs(props)
7
8const doubleCount = computed(() => count.value * 2)
9</script>
10
11<template>Double Count: {{ doubleCount }}</template>

toRefs(props) converts a reactive object (in this example props) to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object. We need to use count.value inside the computed property, as it is a ref.

Here's the StackBlitz demo for toRefs:

You can also watch the corresponding video:

If you liked this Vue tip, follow me on Twitter to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:

I will never share any of your personal data. You can unsubscribe at any time.