Watcher doesn't detect modifications in array values

I am working with a basic array structure that looks like this:

dates[2]
  0:"2018-01-09T15:00:00.000Z"
  1:"2018-01-10T14:00:00.000Z"

My watcher is configured as follows:

data() {
    return {        
        dates: [],
    }
}

watch: {
    // this function will run whenever dates changes
    dates: {
        handler: function(before, after) {
            console.log('dates has been changed');
        },
        deep: true
    }
},

Currently, the watcher only detects when an element is added or removed from the array, not when the value (time) changes.

Is there a way to achieve the desired behavior?

Answer №1

Because of the way JavaScript works, Vue has difficulty detecting certain changes to arrays:

If you directly replace an item using its index, such as vm.items[indexOfItem] = newValue

If you change the length of the array, for example, vm.items.length = newLength

To address the first issue, the following two methods achieve the same result as vm.items[indexOfItem] = newValue, while also triggering state updates in the reactivity system:

// Using Vue.set
Vue.set(example1.items, indexOfItem, newValue)

// Using Array.prototype.splice
example1.items.splice(indexOfItem, 1, newValue)

To handle the second issue, you can use splice:

example1.items.splice(newLength)

Source: https://v2.vuejs.org/v2/guide/list.html#Caveats

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

Transmit information from node.js to the frontend utilizing only raw JavaScript

Seeking guidance on sending data object from a Node.js server to a JS file for frontend use. In the main.js file, DOM manipulation is performed. The following request is made: let dataName = []; let request = async ('http://localhost:3000/') =& ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

Activate Cross-Origin Resource Sharing for GraphQL requests

Currently working on a project that involves graphQL and spring boot. The API functions properly when using graphiQL, but there's an issue with CORS origin error when trying to consume it with Apollo vueJS. In the ProductQuery class which implements ...

Clicking on a span will allow you to alter the text within a paragraph located in a

I am hoping to update a paragraph of text with new content when a faux 'link' (span) at the bottom of the page is clicked. This is what I have so far <body> <p> This is the paragraph that should be changed after clicking on the sp ...

Transferring an Array from PHP to Javascript

I'm attempting to pass a PHP array so that I can use it in JavaScript. The PHP code I have written is shown below: <?php $link = mysqli_connect("localhost", "root", "password", "database"); /* check connection */ if (mysqli_connect_errn ...

node js retrieves information from the request body

Hey there! I'm diving into the world of Node.js and JavaScript, but I've hit a roadblock. I'm trying to fetch data from a URL using node-fetch, then parse it as JSON. However, I keep running into the issue of getting 'undefined' in ...

Determine the number of rows in an Excel spreadsheet using JavaScript

Currently, I am working on a codeigniter project where I need to upload an Excel file. However, before uploading the file, I want to display the number of records it contains. To achieve this, I decided to create a function within my script and then call t ...

Utilize the fetch function within a React functional component

I have been experimenting with different methods to fetch data only once before rendering, but I am encountering some challenges: It is not possible to call dispatch in componentDidMount as there is a restriction that it can only be done in Functional c ...

Can you provide me with the URL for the jQuery post function?

Could someone please clarify which URL I should use in the $.post call to the server for a node.js file? Most tutorials demonstrate with PHP files, but I'm unsure about calling node.js files. Should I post it to the app.js file or the route file? Thi ...

Alter the class of the div element every three seconds

Greetings, I trust everyone is doing well. I am in need of some assistance from your brilliant minds. I have a circular div along with three CSS classes, and my objective is to switch the div's class and update the label text every 3 seconds. Any insi ...

It appears that using "object[prop]" as a name attribute does not function properly in HTML

After using console.log on the req.body I received this output: [Object: null prototype] { 'shoe[name]': 'Shoe Name', 'shoe[description]': '', 'shoe[pictures]': '', 'shoe[collections] ...

Can you outline the key distinctions between AngularJS and ReactJS?

Looking to create a website that will be converted into a mobile application, I realize that my expertise lies more in desktop and Android native development rather than web client side development. After some research, I have decided to utilize HTML5, CSS ...

Do not directly change a prop because it will be replaced with a new value whenever the parent component is re-rendered

Operating a blog that fetches posts and their titles via an API. Using Axios queries, I have enabled the ability to delete and modify these posts. Upon clicking the "change post" button, a page opens displaying the title and post with editable text fields. ...

Navigating TS errors when dealing with child components in Vue and Typescript

Recently, I encountered an issue where I created a custom class-based Vue component and wanted to access its methods and computed properties from a parent component. I found an example in the Vue Docs that seemed to address my problem (https://v2.vuejs.org ...

Tips for incrementing a number by 1 at random intervals using jQuery

Is there a way to increase a number by 1 after an unpredictable delay? For instance, starting with a base value of 10, can we have it change by 1 after a random amount of time (e.g. between 1 and 5 seconds)? I attempted the following code: var ...

What is the process for retrieving paginated data from the store or fetching new data from an API service within an Angular 2 application using ngrx-effects?

After coming across this insightful question and answer about the structure of paginated data in a redux store, I found myself pondering how to implement similar principles using ngrx/store in an angular 2 application. { entities: { users: { 1 ...

What is the importance of having the same data type for the searchElement in the argument for Array.prototype.includes()?

Is there an issue with my settings or is this a feature of TypeScript? Consider the code snippet below: type AllowedChars = 'x' | 'y' | 'z'; const exampleArr: AllowedChars[] = ['x', 'y', 'z']; f ...

Defining optional parameters in TypeScript

Currently, I am working on implementing strong typing for a flux framework (specifically Vuex). Here is my current code: const actions = { first(context: Context, payload: string) { return doSomething(context, payload); }, second(context: Context) { r ...

Personalized modify and remove elements on a row of the DataGrid material-ui version 5 component when hovered over

In my React Js app, I am utilizing Material UI components or MUI v5 as the UI library for my project. Within the DataGrid/DataGridPro component, I am implementing a custom edit and delete row feature. The requirement is to display edit and delete icons w ...

Encapsulating the React Material-ui Datepicker and Timepicker OnChange event callback functionging

I'm new to React and currently incorporating Material-UI into my project for additional UI components. I've noticed some repetitive code that I'm finding difficult to refactor due to constraints within a Material-UI component's implemen ...