Using Vue2: It is recommended to refrain from directly mutating a prop inside a component

When attempting to determine if a person's input should be considered invalid due to being empty, I keep encountering the error message "Avoid mutating a prop directly".

<script type="text/x-template" id="srx">
  <input type="number" name="persons" id="persons" value="" v-model="persons" :class="{invalid: !persons}">

</script>


<div id="app">
  {{stepText}}
  <sr-el :persons="1"></sr-el>

  <button v-if="currentStep > 1" type="button" v-on:click="previous()">prev</button>
  <button v-if="currentStep < 6" type="button" :disabled="currentStepIsValid()" v-on:click="next()">
    next</button>
</div>

Vue.component('sr-el', {
  template: '#srx',
  props: ['persons'],
})


var App = window.App = new Vue({
  el: '#app',
  data: {
    persons: '1'
    currentStep: 1,
  },
  methods: {
    currentStepIsValid: function() {

      if (this.currentStep == 1) {
        this.stepText = "Step 1;
        return !(this.persons > 0);
      }

    },
    previous: function() {
      this.currentStep = this.currentStep - 1;
      // prev slide
    },

    next: function() {
      this.currentStep = this.currentStep + 1;
      // next slide

    }
  }
})

Answer №1

The reason for the warning message is that you are binding persons to the input within the template using v-model. Any changes made to the input will directly affect the value of persons, causing the prop to be mutated within your sr-el component.

To resolve this issue, it is recommended to create a new property and assign the value of persons to it, then use this new property with v-model:

Vue.component('sr-el', {
  template: '#srx',
  props: ['persons'],
  data: function() {
    return {
      inputVal: this.persons,
    }
  }
})
<input v-model="inputVal" ... />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve a document from a collection in a Meteor application?

My experience with mongodb is very limited, so I need help on how to retrieve a document from a meteor collection. I am trying to check if a document exists for the user and update it with an object. if (Saves.find({_id: Meteor.userId()}).fetc ...

Shuffle the elements in an array while preserving the original sequence

I am trying to find a way to randomize a list of email addresses, remove any duplicates, and still maintain the original order. I have successfully achieved each task individually but struggle when combining them all together. Below is my attempt which i ...

Create reactivity in output by utilizing global functions in Vue

I've recently dived into Vue, along with vue-cli and Single File Components. I'm facing a challenge where I need to have a global function that provides formatted text to components throughout my application. This text should be based on the curr ...

Creating a new application to manage and oversee my mathematics classes

As a mathematician with experience in programming, I run a successful (Math) YouTube channel and offer paid math courses. I am looking to create a web application or customize an existing template that will enable me to manage access to my classes (priva ...

Is it possible to retry NTLM Authentication failure in Sails.JS?

Currently, my NodeJS application is built on Sails.JS and uses ntlm-express for NTLM authentication. Everything works smoothly when the authentication is successful. However, when it fails (such as when a Firefox user enters incorrect credentials), ntlm-ex ...

Incorporating Vue and Vite into a pre-existing PHP project

Although I have a strong background in PHP and Javascript, I am a complete beginner when it comes to modern js frameworks like Vue, Angular, or React. I've also never worked with bundlers before. Recently, I decided to dive into the world of modern w ...

The 'load()' event method in jQuery does not function properly within the 'ready()' event method on mobile browsers

After testing on Firefox mobile and Chrome mobile browsers, I found that my code works perfectly on computer browsers. Below is the code I have inside the ready() and load() event methods: $(document).ready(function(){ $(window).on('load hashchan ...

How to modify attributes using ng-content in Angular 2

Looking for a way to modify the attribute of the top div within the ng-content in my code. Here's an example snippet: <ng-container> <ng-content select="[content-body]"></ng-content> </ng-container> For instance, I want t ...

Identify the changed field using Javascript

In my MVC application, I have a form with multiple fields that I am monitoring for changes using JavaScript. The code snippet below keeps track of any changes made in the form: var _changesMade = false; // Flag the model as changed when any other fie ...

How can a Chrome extension automatically send a POST request to Flask while the browser is reloading the page?

I am looking to combine the code snippets below in order to automatically send a post request (containing a URL) from a Chrome extension to Flask whenever a page is loading in Chrome, without needing to click on the extension's icon. Is this feasible? ...

Tips for postponing submission of a form in Prestashop

In the process of creating a carrier module for Prestashop, I have integrated two radio buttons (now,schedule). When the user selects now, the current datetime is obtained. However, if the user chooses schedule, a calendar prompt appears to select a dateti ...

Implementing Firebase pagination alongside Mui-Datatable pagination and sorting capabilities

I've been trying to implement server-side pagination and filtering for the Mui-Datatable to display my data, but I haven't been successful so far. Here are the snippets of code that I have been working on: One issue that I'm facing is that ...

Is there a way to transform array and object into a new result in JavaScript by inverting them?

Embarking on a new project, I find myself in unfamiliar territory. The data at hand is structured as follows: { domain1.com: [ { city: "New-York" }, { city: "Denver" }, { city: "Las-Vegas" }, { city: "Bo ...

Learn how to retrieve data prior to rendering with Vue 3 and the composition api

Is there a way to fetch data from an API and populate my store (such as user info) before the entire page and components load? I have been struggling to find a solution. I recently came across the beforeRouteEnter method that can be used with the options ...

Mobile view on Google Maps app

I am currently using Google Map API V3 and for styling, I have integrated Snazzy Maps. I have successfully added multiple markers for different locations and everything seems to be working fine. However, there is an issue with mobile devices where users ne ...

Tips for effectively incorporating Cook-Torrance shading in three.js?

I've been working on integrating the Cook-Torrance shading algorithm into my three.js project. I've made progress, but I'm experiencing issues with the ambient light not displaying correctly. The sections of the cube that are not directly li ...

Having trouble understanding how to receive a response from an AJAX request

Here is the code that I am having an issue with: render() { var urlstr : string = 'http://localhost:8081/dashboard2/sustain-master/resources/data/search_energy_performance_by_region.php'; urlstr = urlstr + "?division=sdsdfdsf"; urlst ...

Transforming a div into a clickable hyperlink

I have a JavaScript function that generates a div with a link inside it. Here is how the div and link are created: $('#info').find('#link').text("View"); //Creates a new link var eventLink1 = document.createElement('a& ...

What are the best practices for implementing media queries in Next.js 13.4?

My media queries are not working in my next.js project. Here is what I have tried so far: I added my queries in "styles.module.css" and "global.css" and imported them in layout.js I included the viewport meta tag under a Head tag in layout.js This is how ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...