--- title: Never Mutate Computed Property Return Values impact: HIGH impactDescription: Mutating computed values causes silent failures and lost changes type: capability tags: [vue3, computed, reactivity, immutability, common-mistake] --- # Never Mutate Computed Property Return Values **Impact: HIGH** - The returned value from a computed property is derived state - a temporary snapshot. Mutating this value leads to bugs that are difficult to debug. **Important:** Mutations DO persist while the computed cache remains valid, but are lost when recomputation occurs. The danger lies in unpredictable cache invalidation timing - any change to the computed's dependencies triggers recomputation, silently discarding your mutations. This makes bugs intermittent and hard to reproduce. Every time the source state changes, a new snapshot is created. Mutating a snapshot is meaningless because it will be discarded on the next recalculation. ## Task Checklist - [ ] Treat computed return values as read-only - [ ] Update the source state instead of the computed value - [ ] Use writable computed properties if bidirectional binding is needed - [ ] Avoid array mutating methods (push, pop, splice, reverse, sort) on computed arrays **Incorrect:** ```vue ``` ```vue ``` **Correct:** ```vue ``` ```vue ``` ## Writable Computed for Bidirectional Binding If you genuinely need to "set" a computed value, use a writable computed property: ```vue ``` ## Reference - [Vue.js Computed Properties - Avoid Mutating Computed Value](https://vuejs.org/guide/essentials/computed.html#avoid-mutating-computed-value) - [Vue.js Computed Properties - Writable Computed](https://vuejs.org/guide/essentials/computed.html#writable-computed)