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/ts-withdefaults-mutable-factory-function.md
1---2title: Wrap Mutable Default Values in Factory Functions3impact: HIGH4impactDescription: Without factory functions, all component instances share the same mutable reference causing cross-contamination bugs5type: gotcha6tags: [vue3, typescript, props, withDefaults, mutable-types]7---89# Wrap Mutable Default Values in Factory Functions1011**Impact: HIGH** - When using `withDefaults()` with type-based props declaration, default values for mutable types (arrays and objects) MUST be wrapped in factory functions. Without this, all component instances share the same reference, causing bugs where modifying the prop in one instance affects all others.1213## Task Checklist1415- [ ] Always wrap array defaults in factory functions: `() => []`16- [ ] Always wrap object defaults in factory functions: `() => ({})`17- [ ] Primitive types (string, number, boolean) do NOT need factory functions18- [ ] Review existing components for this pattern1920## The Problem: Shared Mutable References2122**WRONG - Shared reference across instances:**23```vue24<script setup lang="ts">25interface Props {26items?: string[]27config?: { theme: string }28}2930const props = withDefaults(defineProps<Props>(), {31items: ['default'], // WRONG! All instances share this array32config: { theme: 'light' } // WRONG! All instances share this object33})34</script>35```3637When you have multiple instances of this component:38```vue39<template>40<!-- Both share the SAME items array! -->41<MyComponent ref="comp1" />42<MyComponent ref="comp2" />43</template>4445<script setup>46// If comp1 modifies its items, comp2's items change too!47comp1.value.items.push('new item') // comp2 also has 'new item' now48</script>49```5051## The Solution: Factory Functions5253**CORRECT - Unique instance per component:**54```vue55<script setup lang="ts">56interface Props {57items?: string[]58config?: { theme: string }59nested?: { data: { values: number[] } }60}6162const props = withDefaults(defineProps<Props>(), {63items: () => ['default'], // Factory function!64config: () => ({ theme: 'light' }), // Factory function!65nested: () => ({ data: { values: [] } }) // Factory function!66})67</script>68```6970## When Factory Functions Are Required7172| Type | Factory Required | Example Default |73|------|-----------------|-----------------|74| `string` | No | `'hello'` |75| `number` | No | `42` |76| `boolean` | No | `false` |77| `string[]` | **Yes** | `() => []` |78| `number[]` | **Yes** | `() => [1, 2, 3]` |79| `object` | **Yes** | `() => ({})` |80| `Map` | **Yes** | `() => new Map()` |81| `Set` | **Yes** | `() => new Set()` |82| `Date` | **Yes** | `() => new Date()` |8384## Complete Example8586```vue87<script setup lang="ts">88interface User {89id: string90name: string91}9293interface Props {94// Primitives - no factory needed95title?: string96count?: number97disabled?: boolean9899// Mutable types - factory required100items?: string[]101users?: User[]102metadata?: Record<string, unknown>103selectedIds?: Set<string>104}105106const props = withDefaults(defineProps<Props>(), {107// Primitives108title: 'Default Title',109count: 0,110disabled: false,111112// Mutable types with factory functions113items: () => [],114users: () => [],115metadata: () => ({}),116selectedIds: () => new Set()117})118</script>119```120121## Vue 3.5+ Reactive Props Destructure122123Vue 3.5 introduces reactive props destructure, which handles this automatically:124125```vue126<script setup lang="ts">127interface Props {128items?: string[]129config?: { theme: string }130}131132// Vue 3.5+ - defaults work correctly without explicit factory functions133const {134items = ['default'], // Each instance gets its own array135config = { theme: 'light' } // Each instance gets its own object136} = defineProps<Props>()137</script>138```139140Note: Under the hood, Vue 3.5 handles the instance isolation for you.141142## Common Bug Pattern143144This bug often appears in list/table components:145146```vue147<!-- ListItem.vue - BUGGY -->148<script setup lang="ts">149interface Props {150selectedRows?: number[]151}152153// All ListItems share the same selectedRows array!154const props = withDefaults(defineProps<Props>(), {155selectedRows: [] // BUG: Missing factory function156})157</script>158```159160Users report: "Selecting a row in one table selects it in all tables!"161162**Fix:**163```typescript164const props = withDefaults(defineProps<Props>(), {165selectedRows: () => [] // Now each instance has its own array166})167```168169## Reference170- [Vue.js TypeScript with Composition API - Default Props](https://vuejs.org/guide/typescript/composition-api.html#props-default-values)171- [Vue RFC - Reactive Props Destructure](https://github.com/vuejs/rfcs/discussions/502)172