What is the reason for the watcher triggering twice?

Utilizing vue-material.io for my project, specifically the datepicker component.

Encountering an issue with state observation.

Noticing that the watcher is triggering twice. Is this a bug in the package or inherent to Vue?

Steps to Replicate:

Attempt to select a date and monitor the console output.

You'll notice that it triggers unexpectedly twice.

Link to CodeSandbox Example

Answer №1

It seems like the repetitive triggering of the watcher in the component is causing this issue. When the date is modified by a click, the data is updated and emitted. Subsequently, the internal watcher interprets this change, formats the data, and emits it again. This results in the watcher being activated twice.

Although the behavior may seem peculiar, it is actually functioning as designed.

For further insights, you can review the source code at: https://github.com/vuematerial/vue-material/blob/dev/src/components/MdDatepicker/MdDatepicker.vue

Answer №2

One approach is to assess the difference between oldVal and newVal, triggering an event only when there is a change in value

  watch: {
    selectedDate(newVal, oldVal) {
      if (!newVal || !oldVal || newVal.getTime() !== oldVal.getTime()) {
        console.log(`this is ${newVal}`);
      }
    }
  }

Answer №3

To verify, you can use the following method:

if (!value || `${value.getHours()}${value.getMinutes()}${value.getSeconds()}` === '000') {
    // place your code here
    console.log(`the value is ${value}`);
}

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

use javascript to color the image based on a specified number or percentage

Looking for a method to dynamically fill an image based on a percentage. Is there a dynamic approach or is it simpler to create images at specific intervals? I am working with Angular and need to set the percentage of a 1 to 5 rating system from AJAX. h ...

Notification Click Event for PWA Service Worker

I am attempting to display a notification and trigger an action when it is clicked. try { navigator.serviceWorker.getRegistration() .then(reg => { reg.showNotification("Check out the video clip!", { body: "Cl ...

What are the best strategies for handling complex task operations in Node.js Express.js?

How can I effectively manage lengthy task functions in Node.js Express.js to prevent timeout errors? Currently, my application includes a time-consuming function that does not require an immediate response but still needs to execute its tasks. How can I en ...

Guide on navigating an array of objects using the provided keys as a starting point in Javascript/Typescript

Assuming I have an array of objects structured like this: const events: Array<{year: number, month: number, date: number}> = [ {year: 2020, month: 10, date: 13}, {year: 2021: month: 3, date: 12}, {year: 2021: month: 9, date: 6}, {year: 2021: mont ...

basic tiptap add-on or advanced prosemirror module

Exploring the possibilities of utilizing the tiptap editor for Vue.js in conjunction with the Prosemirror editor has sparked my interest. I've delved into a lot of information about tiptap, however, I must admit that the documentation is not as compr ...

Using Selenium with Python to interact with a dynamically generated object that does not have an identifying id or name

Whenever a Right click is performed, an options popup/screen will appear with dynamic content/element. Here is the Options Popup: https://i.stack.imgur.com/TfxpC.png This dynamic content/element will disappear as soon as the mouse is clicked elsewhere o ...

generateError('Circular reference found' + nodeRep)

Incorporating numerous charts in my application, I aim to manage all chart colors from the vuex store. Implementing the following code has allowed me to achieve this functionality. store.js import Vue from 'vue' import Vuex from 'vuex&apos ...

Customizing background styles in real-time in a Meteor-Angular application

I'm currently working on a unique AngularJS Ionic Meteor application, and I am in search of a method to dynamically change the background color of the bottom box within an ionic card based on specific float values. The criteria for color changes are a ...

Display the chosen rows from an HTML table

I currently have a table set up like the one shown in this example. I am now looking to add a feature that allows users to print only selected rows. These rows should be selectable by clicking on checkboxes located on the right side of each row. If anyone ...

Anticipating the arrival of data from an unspecified child component prior to executing the effect

Suppose there is a parent component: function Parent({ children }) { useEffet(() => { // Perform action only after all children have returned their values via props (or context) }) return <div>{ children }</div> } How do I ensure ...

The functionality to refresh an input field upon clicking a button is not functioning as expected

I am currently developing a small MVC.NET web application with user input functionality. The goal is to calculate and display the results in two input fields upon button click. However, I am facing an issue where the input fields remain empty after the but ...

Vuex is exclusively eliminating the initial element

Currently, I have set up the code to remove the activeNote from the array called notes by using the DELETE_NOTE mutation. However, it seems that it only removes the first element of the array. The content of mutations.js is as follows: export const mutat ...

The unpredictable number is malfunctioning and is only showing up on the initial element

I'm facing an issue with my code. The scenario is, upon clicking the first door, an image opens based on a random number condition; for example, 1 equals an empty door and 2 equals finding a person behind it. Now, when I click on the second or third ...

Tips for maintaining the persistent state of tabs in a React.js application

I have created dynamic tabs using an array list, including nested tabs within those tabs. You can view the live sandbox link here: For the sandbox code and preview, visit: https://codesandbox.io/s/black-glade-phs69 The setup consists of 3 main tabs ...

Trouble with Jsonp when using the getJSON function in jQuery

$(document).ready(function() { $.getJSON("http://quanta.net16.net/wordpressnew/test.php?jsoncallback=?", function(data) { alert('swag'); }); }); This is the JSON request I'm making and my data is properly contained within ?({object} ...

Passing parameters to Next.js pages

Here is my function: export async function getServerSideProps({ req }: any) { const user = ( await axios.get("http://localhost:4000/api/auth/status", { withCredentials: true, headers: { Cookie: `connect.sid=${req.cookies["c ...

How to send a global variable to an event callback function in JavaScript?

I have encountered an issue in the following code where I am attempting to pass mydata to the callback function. This is just a small part of a larger problem that I am facing and I suspect it may be related to scope. Can anyone help me identify what is wr ...

Having Trouble Creating the production APK in React Native

Having trouble generating an APK after following the steps outlined in the documentation. Build Failed even though I tried: 1. cd android 2. gradlew clean "build Successful" 3. gradlew assembleRelease "BUILD FAILED" with this Erro ...

"A TypeError was not caught when attempting to operate on a null value

Currently, I am using AJAX to generate a tabular format on my webpage. Everything was working fine until I encountered the following problem: Snippet of My Code $.each(res,function(index,row){ console.log(row.bookable); //{id: 2, sl ...

Focusing on the active element in Typescript

I am working on a section marked with the class 'concert-landing-synopsis' and I need to add a class to a different element when this section comes into focus during scrolling. Despite exploring various solutions, the focused variable always seem ...