Vue Tip: Destructure Props in Composition API Without Losing Reactivity
Inside <script setup>
we use the defineProps()
compiler macro to access props:
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:
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:
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:
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 X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:
Nuxt Tip: Analyse Production Bundle
Nuxi analyze is an experimental feature that builds your Nuxt app and analyses the production bundle.
Vue Tip: Re-Rendering Vue Routes When Path Parameters Change
In SPA that uses the Vue Router, it’s common to create a path parameter that changes the behavior of a route.