Back
VueMediumNew
Why shouldn't you mutate props directly, and what should you do instead?
One-way data flow
Ideal Answer
Props establish a one-way data flow from parent to child. If a child mutates a prop directly, the change won't propagate back correctly and can cause confusing bugs when the parent later re-renders and overwrites the child's local change — Vue also emits a runtime warning for this.
Instead:
- Create local state derived from the prop (e.g., with
ref) if the child needs its own copy. - Emit an event so the parent updates the source of truth, following Vue's unidirectional flow.
JavaScript
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
function updateValue(v) {
emit('update:modelValue', v)
}