--- title: Props Are Read-Only - Never Mutate Props impact: HIGH impactDescription: Mutating props breaks one-way data flow and causes unpredictable parent-child state synchronization issues type: gotcha tags: [vue3, props, one-way-data-flow, mutation, component-design] --- # Props Are Read-Only - Never Mutate Props **Impact: HIGH** - Props in Vue follow one-way data flow: parent to child only. Mutating a prop in a child component violates this contract, triggering Vue warnings and causing hard-to-debug state synchronization issues. The parent component loses control of the data it passed down. Props are "snapshots" from the parent at render time. Vue's reactivity system tracks props at the parent level - mutating in the child doesn't notify the parent, leading to state inconsistencies. This is especially dangerous with object/array props because JavaScript passes them by reference, allowing mutation without assignment. ## Task Checklist - [ ] Never assign new values to props - [ ] Never mutate object or array prop properties directly - [ ] Use emit to request parent to make changes - [ ] Create local copies if you need to modify prop-based data - [ ] Use computed properties for derived values ## The Problem **Incorrect - Direct primitive prop mutation:** ```vue ``` **Incorrect - Object/array prop mutation (silent but dangerous):** ```vue ``` This pattern is dangerous because: 1. Parent component doesn't know about the change 2. Data can become out of sync 3. Makes debugging difficult - where did the change come from? 4. Breaks the component contract ## Pattern 1: Emit Events to Parent Let the parent handle all data changes. **Correct:** ```vue ``` ```vue ``` ## Pattern 2: Local Copy for Editable Data (Prop as Initial Value) When the component needs to work with a modified version of prop data. **Correct:** ```vue ``` ## Pattern 3: Computed Properties for Transformations When you need a derived/transformed version of the prop. **Correct:** ```vue ``` ## Pattern 4: v-model for Two-Way Binding For form-like components that need two-way binding. **Correct:** ```vue ``` ```vue ``` ## Common Mistake: Thinking Object Mutation Is Safe ```vue ``` Vue doesn't warn because it can't efficiently detect deep mutations. But this still: - Breaks one-way data flow - Makes the component unpredictable - Causes maintenance nightmares **Always treat props as if they were deeply frozen.** ## Reference - [Vue.js Props - One-Way Data Flow](https://vuejs.org/guide/components/props.html#one-way-data-flow) - [Vue.js Props - Mutating Object/Array Props](https://vuejs.org/guide/components/props.html#mutating-object-array-props)