Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js: Is changing objects passed as props a bad technique?

I noticed that in Vue you could change object fields passed as Props, and it changes these fields in parent.

Is this a bad technique? Should it be avoided, or it may be used? What the caveats of this approach?

Example:

Parent.vue:

<script setup>
import { reactive } from 'vue';
import ChildInput from "./Child.vue";
</script>

<script>
const myObj = reactive({'val':'Default text'});
</script>

<template>
  <ChildInput :passedObj="myObj" />
  <div>
    It's a parent input: 
    <input v-model="myObj.val">
  </div>
</template>

Child.vue:

<script setup>
  const props = defineProps({
    passedObj: {
      type: Object,
      required: true,
    },
  })
  
  const dropValue = function() {
        props.passedObj.val = "Changed text";
  }
</script>

<template>
  <div>
    <label>
      It's a child input: 
      <input v-model="passedObj.val">
      <button @click="dropValue">Change text</button>
    </label>
  </div>
</template>

You can check this example here.

like image 673
Alexander Avatar asked Feb 07 '26 22:02

Alexander


1 Answers

Shallow prop mutation is prohibited because props object is read-only.

Deep prop mutation is a bad practice that should be avoided. One reason is that this makes data flow more complicated and hard to follow, that this happened unconsciously in this case explains why this is a problem. Another possible reason is that the performance can potentially be affected because this scenario lies outside the common use, although I'm unaware of such optimization problems as of now.

The official recommendation is to use v-model two-way binding when props need to be mutated, so the mutation happens in parent component and can be tracked through Vue events when debugging is needed. When a prop is deeply mutated, it's cloned in a child and emitted to a parent.

like image 157
Estus Flask Avatar answered Feb 09 '26 12:02

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!