Vue.js Developer interview question · Most asked
Why does destructuring sometimes lose reactivity in Vue, and how do you avoid it?
Summary
Reactivity is tracked through property access on a proxy or .value on a ref. Destructuring a reactive() object, a store, or (before Vue 3.5) props copies the current values into plain variables, which no longer update.
Longer Answer
Fixes: keep the object and access properties (state.count), use toRefs() / toRef() to get refs per property, storeToRefs() for Pinia stores, and in Vue 3.5+ props destructuring in <script setup> is compiled to keep reactivity. When passing a reactive value into a composable, pass a ref or getter (() => props.id) rather than the raw value, and read it with toValue().
Key Points
- Destructuring copies values.
- toRefs / toRef / storeToRefs.
- Props destructure reactive in 3.5+.
- Pass refs or getters to composables.
More most-asked Vue.js Developer questions
- Options API or Composition API: what is the difference, and which would you use?
- How do props work, and what is one-way data flow?
- How does Vue 3's reactivity system work, and how does it differ from Vue 2's?
- computed, watch or watchEffect: when do you use each?
- ref or reactive: what is the difference, and when do you use each?
- How do you use Vue Router: dynamic routes, nested routes and navigation guards?
- Why does v-for need a key, and why should it not be the index?
- What are composables, and why did they replace mixins?