JavaScript Vue2: warning: Avoid mutating a prop directly

I'm stuck in the situation where my child component (autocomplete) needs to update a value of its parent (Curve), And the parent needs to update the one of the child (or to completely re-render when a new Curve component is used)

In my app the user can select a Curve in a list of Curve components. My previous code worked correctly except the component autocomplete was not updated when the user selected another Curve in the list (the component didn't update its values with the value of the parent).

This problem is fixed now but I get this warning:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value"

The description of this warning explain exactly what behavior I expect from my components. Despite this warning, this code works perfectly fine !

Here is the code (parts of it have been removed to simplify)

// curve.vue
<template>
  <autocomplete v-model="curve.y"></autocomplete>
</template>


<script>
  import Autocomplete from './autocomplete'

  export default {
    name: 'Curve',
    props: {
      value: Object
    },
    computed: {
      curve() { return this.value }
    },
    components: { Autocomplete }
  }
</script>


// autocomplete.vue
<template>
    <input type="text" v-model="content"/>
</template>

<script>
  export default {
    name: 'Autocomplete',
    props: {
      value: {
        type: String,
        required: true
      }
    },
    computed: {
      content: {
        get() { return this.value },
        set(newValue) { this.value = newValue }
      }
    }
  }
</script>

A lot of people are getting the same warning, I tried some solutions I found but I was not able to make them work in my situation (Using events, changing the type of the props of Autocomplete to be an Object, using an other computed value, ...)

Is there a simple solution to solve this problem ? Should I simply ignore this warning ?

Answer:1

You can ignore it and everything will work just fine, but it's a bad practice, that's what vue is telling you. It'll be much harder to debug code, when you're not following the single responsibility principle.

Vue suggests you, that only the component who owns the data should be able to modify it.

Not sure why events solution ($emit) does not work in your situation, it throws errors or what?

To get rid of this warning you also can use .sync modifier: https://vuejs.org/v2/guide/components.html#sync-Modifier

Answer:2

I have written following jQuery for calculating age when user selects date from date picker and the focus of textbox(#dobfield) is lost then the calculated age should get displayed on textbox (#age). ...

I have written following jQuery for calculating age when user selects date from date picker and the focus of textbox(#dobfield) is lost then the calculated age should get displayed on textbox (#age). ...

  1. display selected date in datepicker
  2. display selected date from calendar into textbox
  3. display selected date in textbox in asp.net
  4. display selected date in jquery
  5. power bi display selected date range
  6. jquery datepicker display selected date
  7. show selected date in datepicker
  8. how to show selected date in datepicker
  9. selected date in jquery datepicker
  10. selected date in datepicker

I am trying do cache busting using webpack by adding a hash to the end of each javascript file. My webpack configuration file is below: const AssetsPlugin = require('assets-webpack-plugin'); const ...

I am trying do cache busting using webpack by adding a hash to the end of each javascript file. My webpack configuration file is below: const AssetsPlugin = require('assets-webpack-plugin'); const ...

  1. webpack cache busting index.html
  2. webpack cache busting css
  3. webpack cache busting images
  4. webpack cache busting
  5. webpack cache busting js
  6. cache busting webpack
  7. webpack 4 cache busting
  8. react webpack cache busting
  9. webpack bundle cache busting
  10. webpack chunk cache busting
  11. angular webpack cache busting
  12. laravel webpack cache busting
  13. webpack disable cache busting
  14. vue webpack cache busting
  15. webpack html cache busting
  16. webpack 2 cache busting
  17. wordpress webpack cache busting
  18. webpack dynamic import cache busting
  19. angular cli webpack cache busting
  20. html-webpack-plugin cache busting

I updated the ui-bootstrap version from 0.13.0 to 0.13.4, It broke the validation on datepicker field. Plunkr with a problem Previously I was using 0.13.0 and it was working fine. Basically when I ...

I updated the ui-bootstrap version from 0.13.0 to 0.13.4, It broke the validation on datepicker field. Plunkr with a problem Previously I was using 0.13.0 and it was working fine. Basically when I ...

I have some blocker. How can i select character and sort this inside a tag like <p>-tag in jQuery OR Vanilla JS? function alphabet(str){ var x = str.split(" "); return x.sort()....

I have some blocker. How can i select character and sort this inside a tag like <p>-tag in jQuery OR Vanilla JS? function alphabet(str){ var x = str.split(" "); return x.sort()....