Question presented to candidate:
"In a Vue 3 <script setup> component, when would you use ref and when reactive? Walk me through what each one actually returns and the bugs you have seen from picking the wrong one."
What a strong answer should cover:
ref(x)returns a ref object with a tracked.value; it holds any type, and an object placed in it is made deeply reactive.reactive(obj)returns a deep Proxy of the object itself; it only accepts objects (a primitive gets a dev warning and comes back unchanged).- A ref can be reassigned as a whole (
list.value = [...]); areactivevariable cannot, because effects keep tracking the old proxy. - Destructuring a reactive object copies values and loses reactivity;
toRefsfixes that. Refs survive being passed around and returned from composables. - Unwrapping rules: top-level refs unwrap in templates, refs nested in a reactive object unwrap on access, refs inside reactive arrays and Maps do not.
- The Vue docs recommend
ref()as the primary API;reactivestill fits a cohesive object such as a form you never replace.
Clarifying questions expected:
- "Is this for local component state, or for state shared through a composable or store?"
- "Is the team on TypeScript? That changes how much
.valuematters in practice."
Code / implementation expected: Yes β show reassignment and destructuring with both, and what the template displays.