Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does watcher trigger twice?

I'm using vue-material.io, the datepicker particularly.

But I've got a problem during state observation.

The watcher triggers twice. Is this a package bug or vue's nature?

How to reproduce:

Try to select a date and see console.

You can see it triggers twice unexpectedly.

https://codesandbox.io/s/vue-material-basic-example-hxzwp

like image 369
mending3 Avatar asked Nov 07 '22 15:11

mending3


1 Answers

I think this has to do with a watcher inside the component. If you change the date by clicking it, it sets the data and emits it. Then the internal watcher kicks in and also changes the data by formatting it and emits the value again. This way you watcher gets triggered twice.

The behaviour can feel strange but it's correct.

If you want you can check out the source code here: https://github.com/vuematerial/vue-material/blob/dev/src/components/MdDatepicker/MdDatepicker.vue

like image 61
Keyboard ninja Avatar answered Nov 14 '22 23:11

Keyboard ninja