--- title: Wrap Mutable Default Values in Factory Functions impact: HIGH impactDescription: Without factory functions, all component instances share the same mutable reference causing cross-contamination bugs type: gotcha tags: [vue3, typescript, props, withDefaults, mutable-types] --- # Wrap Mutable Default Values in Factory Functions **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. ## Task Checklist - [ ] Always wrap array defaults in factory functions: `() => []` - [ ] Always wrap object defaults in factory functions: `() => ({})` - [ ] Primitive types (string, number, boolean) do NOT need factory functions - [ ] Review existing components for this pattern ## The Problem: Shared Mutable References **WRONG - Shared reference across instances:** ```vue ``` When you have multiple instances of this component: ```vue ``` ## The Solution: Factory Functions **CORRECT - Unique instance per component:** ```vue ``` ## When Factory Functions Are Required | Type | Factory Required | Example Default | |------|-----------------|-----------------| | `string` | No | `'hello'` | | `number` | No | `42` | | `boolean` | No | `false` | | `string[]` | **Yes** | `() => []` | | `number[]` | **Yes** | `() => [1, 2, 3]` | | `object` | **Yes** | `() => ({})` | | `Map` | **Yes** | `() => new Map()` | | `Set` | **Yes** | `() => new Set()` | | `Date` | **Yes** | `() => new Date()` | ## Complete Example ```vue ``` ## Vue 3.5+ Reactive Props Destructure Vue 3.5 introduces reactive props destructure, which handles this automatically: ```vue ``` Note: Under the hood, Vue 3.5 handles the instance isolation for you. ## Common Bug Pattern This bug often appears in list/table components: ```vue ``` Users report: "Selecting a row in one table selects it in all tables!" **Fix:** ```typescript const props = withDefaults(defineProps(), { selectedRows: () => [] // Now each instance has its own array }) ``` ## Reference - [Vue.js TypeScript with Composition API - Default Props](https://vuejs.org/guide/typescript/composition-api.html#props-default-values) - [Vue RFC - Reactive Props Destructure](https://github.com/vuejs/rfcs/discussions/502)