Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/refs-in-collections-need-value.md
1---2title: Refs in Arrays and Collections Require .value3impact: MEDIUM4impactDescription: Refs inside reactive arrays, Maps, or Sets are NOT auto-unwrapped like in reactive objects5type: capability6tags: [vue3, reactivity, ref, arrays, collections, unwrapping]7---89# Refs in Arrays and Collections Require .value1011**Impact: MEDIUM** - Unlike when a ref is a property of a reactive object, refs inside reactive arrays, Maps, and Sets are NOT automatically unwrapped. You must access them with `.value`, and forgetting this leads to silent bugs.1213Vue only auto-unwraps refs when they are properties of reactive objects. When refs are elements in arrays or values in Maps/Sets, they remain as ref objects and require explicit `.value` access.1415## Task Checklist1617- [ ] Always use `.value` when accessing refs stored in reactive arrays18- [ ] Always use `.value` when accessing refs stored in reactive Maps or Sets19- [ ] Consider storing plain values instead of refs in collections to avoid confusion20- [ ] Be aware of this when iterating over arrays containing refs2122**Incorrect:**23```javascript24import { ref, reactive } from 'vue'2526const books = reactive([ref('Vue 3 Guide')])27const counts = reactive(new Map([['clicks', ref(0)]]))2829// WRONG: Refs in arrays are NOT unwrapped30console.log(books[0]) // Ref object, not 'Vue 3 Guide'31books[0] = 'New Title' // Replaces the ref, doesn't update it!3233// WRONG: Refs in Maps are NOT unwrapped34console.log(counts.get('clicks')) // Ref object, not 035counts.get('clicks')++ // Does nothing useful36```3738**Correct:**39```javascript40import { ref, reactive } from 'vue'4142const books = reactive([ref('Vue 3 Guide')])43const counts = reactive(new Map([['clicks', ref(0)]]))4445// CORRECT: Use .value for refs in arrays46console.log(books[0].value) // 'Vue 3 Guide'47books[0].value = 'New Title' // Updates the ref's value4849// CORRECT: Use .value for refs in Maps50console.log(counts.get('clicks').value) // 051counts.get('clicks').value++ // Increments to 152```5354```javascript55// ALTERNATIVE: Just store plain values in collections (simpler)56const books = reactive(['Vue 3 Guide', 'Vuex Handbook'])57const counts = reactive(new Map([['clicks', 0]]))5859// No .value needed - but changes to individual items aren't independently reactive60console.log(books[0]) // 'Vue 3 Guide'61console.log(counts.get('clicks')) // 06263// Mutations still trigger reactivity through the reactive wrapper64books[0] = 'New Title' // Works65counts.set('clicks', counts.get('clicks') + 1) // Works66```6768```vue69<template>70<!-- In templates, refs in arrays also need special handling -->71<div v-for="(book, index) in books" :key="index">72<!-- If book is a ref, you'd need: -->73{{ book.value }}7475<!-- Or use computed to unwrap them first -->76</div>77</template>78```7980## Reference81- [Vue.js Reactivity Fundamentals - Caveat in Arrays and Collections](https://vuejs.org/guide/essentials/reactivity-fundamentals.html#caveat-in-arrays-and-collections)82