Losing Props Reactivity in Vue: Why Destructuring Breaks and How to Fix ItBrief · digest for October 27
Brief≈ 1 min

Losing Props Reactivity in Vue: Why Destructuring Breaks and How to Fix It

Destructuring has become muscle memory in JavaScript: we write const { count } = props without blinking. In Vue 3's Composition API, though, that habit turns into a sneaky trap.

The props object in Vue is a reactive Proxy. When you destructure a primitive value (a number, string, or boolean), you copy the raw value and sever the link to that proxy: the parent updates the prop, yet your local variable never reacts!

As shown in this breakdown of Vue props reactivity, you can solve this in two ways:

1. The toRefs utility: Wrap the props properties into reactive Ref objects:

import { toRefs } from 'vue';

const props = defineProps<{ count: number }>();
const { count } = toRefs(props);

2. Native destructuring in Vue 3.5+: The Vue 3.5+ compiler preserves reactivity automatically via the macro:

// In Vue 3.5+, reactivity is preserved automatically
const { count = 0 } = defineProps<{ count?: number }>();

For tooling and builds, also check out our look at the Vite+ 1.0 release and VoidZero.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2