Steer clear of directly altering a prop within reusable components

I am facing an issue with reusing a custom dropdown that I have created in my component file where props are the value options in the dropdown. When I try to select the dropdown, I get a Vue warning message:

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

What is the best practice and how should I write this to avoid mutating the prop value?

<template>
  <v-app>
    <SearchSelect
        v-model="newItem.a"
        :options="aList"></SearchSelect>
    <SearchSelect
        v-model="newItem.b"
        :options="bList"></SearchSelect>
    <SearchSelect
         v-model="newItem.c"
         :options="cList"></SearchSelect>
  </v-app>
</template>

<script>
export default {
  name: "Sales",
  data() {
    return {
       aList: [
          { value: "A1", text: "A1" },
          { value: "A2", text: "A2" },
          { value: "A3", text: "A3" },
          { value: "A4", text: "A4" },
          { value: "A5", text: "A5" }
       ],
       bList: [
          { value: "B1", text: "B1" },
          { value: "B2", text: "B2" },
          { value: "B3", text: "B3" }
       ],
       cList: [
          { value: "C1", text: "C1" },
          { value: "C2", text: "C2" },
          { value: "C3", text: "C3" }
       ],
     }
   }
  }
};
</script>

Answer №1

Instead of directly changing the property, it is recommended to trigger an event to notify the parent component. The parent component can then update the value, which will be reflected back down to the child component's property.

The v-model directive in the parent component binds its 'owned' data to the :value property and listens for the @input v-on event.

In this scenario, the responsible component for updating newItem.a should be the one modifying it. However, if SearchSelect is updating it without emitting an event, it might cause issues.

Alternatively, you can utilize state management to eliminate the need for emitting events to the parent component. In this case, the store acts as the single source of truth, and you can use commit or dispatch functions instead of emit to manage state updates.

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

Establish a default route within a Node Express application to handle multiple generic URLs (url/index, url/index2, url/index3, and

Currently, I am in the process of learning React and Express frameworks through exercises provided by NodeSchool.io. My goal is to consolidate all exercise files into a single application with multiple pages named as: index index2 index3 index4 .. ...

Enhancing HTML "range" element with mouse scroll functionality for incrementing values in step increments

I'm working on developing a scroll feature that operates independently from the main window's scrolling function. I aim to trigger specific events in the primary window based on interactions with this separate scrollbar. The only solution I coul ...

Unable to execute click events on JavaScript functions after updating innerHTML using PHP and Ajax

To begin, I want to clarify that I am intentionally avoiding the use of jQuery. While it may simplify things, it goes against the purpose of my project. Please note that 'ajaxFunction' serves as a generic example for AJAX requests using GET/POST ...

Ways to conceal a grid item in Material UI framework

My goal is to hide a specific grid item for a product and smoothly slide the others in its place. Currently, I am using the display:none property but it hides the item instantly. I have already filtered the products and now I want to animate the hiding of ...

In Vue.js with Laravel mix, when the base URL includes a subfolder, here is how you can reference app.js

My website's domain for my Laravel web application is currently set to www.mywebsite.com/my-project/public and I prefer not to change it. After installing Vue.js using npm install && npm run dev, I attempted to reference app.js with the follo ...

How to change from using position: sticky to fixed after scrolling to a specific div

Is there a way to transition the position of sticky content from sticky to Fixed while scrolling down and moving to the next rows, keeping it fixed until just before the footer, where it should scroll again? For example, <!DOCTYPE html> <html ...

Encountered an issue while compiling code using the Istanbul plugin

I'm currently working on generating a code coverage report for my ReactJS project using the babel-istanbul-plugin. However, when I incorporate "istanbul" as a plugin in my .babelrc file and attempt to build, I encounter the following error: ERROR in ...

Vue: The past event target value is returning an empty string when I attempt to paste for the first time

When I paste a string into my input field, it initially appears empty. However, if I paste the same string again, it shows me the previously pasted value. How can I capture the first paste value instead? <template> <v-app> <h3> ...

Attempting to create an array using jQuery's :checked selector

In my table structure below, I have multiple rows with various data: <tr class="row"> <td class="row-checkbox-delete-row"> <input tabindex="-1" class="checkbox-delete-row" type="checkbox" /> </td> <td class="r ...

The direction to the Excel document for conversion into JSON

I have a project in progress where I'm currently working on converting an Excel sheet to JSON. Once the data is converted, it will be displayed using jQuery Datatables on the browser. My code is functioning as expected, but I am encountering an issue ...

Arranging and structuring Handlebars templates

In this particular example... http://example.com/1 ...I am experimenting with organizing a Handlebars template and its corresponding content in separate files, as opposed to having them all combined in the HTML file or JavaScript script, like in this con ...

Identify the appearance of a web component being added to the Document Object

After reading this discussion regarding a web-component I created: <my-vue-web-comp [userId]="1" id="my-component"></my-vue-web-comp> The component functions properly in Angular. How can I determine when the web component h ...

Using v-bind to dynamically set styles in Vue.js

While utilizing v-bind:style to apply dynamic values, I encountered an issue where v-bind:style did not seem to work. However, in the console, I verified that v-bind:style was receiving the correct value (:style='{ color : red (or any other value) }&a ...

Top method for detecting browser closure or navigation to a different page and initiating a logout

In the development of my GWT application, I am facing the challenge of detecting when a user leaves the application or closes the browser window (onUnload event) and initiating a logout process, which involves invalidating the session and performing certai ...

Updating the angular $resource method

I need to customize the query()-method of AngularJS $resource by using a custom $http-GET. However, it doesn't seem to be overriding the operation correctly. The result I am getting is an object with method, data, and headers, instead of data.rows[]. ...

No styles are appearing on a specific element after running a specific jQuery function on that element within a Vue page

I recently integrated JQuery-AsRange (https://github.com/thecreation/jquery-asRange) into my vue.js project. Everything functions as expected within the .vue page, however, I am facing an issue with css styling not being applied. The css styles should be ...

Displaying a group of elements in ReactJS

I'm currently working on assembling an array using different JSX components. There's a function I've created that populates this array with components and then returns it. let components = []; switch (obj.type) { case 'title': ...

What is the average time frame for completing the construction of an electron project?

My project has only a few npm dependencies, but the build process is taking longer than 30 minutes and still counting. I'm not sure if this is normal or if there's an issue causing the delay. I have two specific questions: Is it common for pro ...

Arranging Material UI tabs on both sides

I'm currently working with Material UI tabs and I'm trying to achieve a layout where some tabs are positioned to the left and others to the right. For instance, if I have 5 tabs, I want 3 on the left and 2 on the right. I've tried placing th ...

Minimize a collection of JavaScript objects

I am working with an Array of objects where I need to return the collection as an Object, with the key names being the indexes of their length. Additionally, I have to filter this object based on its values. Check out my code snippet below: const data = ...