Subscribe on changes!

effect fired multiple times and wrong output

avatar
Mar 30th 2023

Vue version

3.2.47

Link to minimal reproduction

https://sfc.vuejs.org/#eNpNUsFunDAQ/ZWRLwsKa9qmpy0btarUL+gtzgHMkHUKtmUbqgjx7xnbsAoXPM8z896b8cp+WcuXGdmFNV46ZQN4DLN9ElpN1rgAKzgcKoetDGrBCocBZYANBmcmOFHpSWihpdE+QAvXmF18KQ+kS0iuLZ5f7rj8lAlwr+97woulvD6tEYeO29nfCBB6izSZvSiOhFhmRuSjeS1Og3LYn6qWL+04Y/XMOe9eKpnDsq6bM31gNIIclfwHKR8eIagJfey2s30tf6TeufDhIUefmdohoDvtDCXJ3+7GjP6dmpONQ+QuaG+khqLjSstx7tETVwnZ6sH+/VsaCdlNjps6r4UWQkHAyY5tQIoAmttj+gOsa7dRNkH1jjW9WvbLZPZv9AgXymy3DSDP8Q8NIF1EXB4djspGaTsHSNqvgtFuBIPwbmPQzSEYTfHP1J2Q3XdRCkbVTX1XyiqWX9J5ai1/80bTW0uWxX7hBSMBWaxg9KJiLNgtBOsvde0H2sSMb54b91rTibtZx51x9NO5c+a/R0eNBTvmxrYPt0jxPA==

Steps to reproduce

click the button and watch console output, the given output was not expected

What is expected?

the effect function should be fired no more than 2 times

What is actually happening?

but fired three times actually

about:srcdoc:160 fired 1 [1] 1
about:srcdoc:160 after (2) [1, 1]
about:srcdoc:160 fired 1 (3) [1, 1, 42] 2
about:srcdoc:160 after (4) [1, 1, 42, 1]
about:srcdoc:160 fired 1 (3) [1, 1, 42] 3
about:srcdoc:160 after (4) [1, 1, 42, 1]

the 2nd and 3rd times is no way to understand.

does vue do some deep duplicate to re-add element to array?

i changed b from array to number,it only fire 2 times.

https://sfc.vuejs.org/#eNplUsFugzAM/RUrF0Bj0Gm3jk6bJu0LdswFUtOmgyRKAtOE+Pc5ELpOO4X37Gc/G0/s1ZhiHJDtWeWElcaDQz+YZ65kb7T1MIHFNrdYCy9HzLFtUXiYobW6h4SkCVdcCa2chxoOITvdZRvT/GPEDQNwVR6PxKdjdnieAg9NYQZ3JoKrOTRY+6bplhBkusOi06c0aaXFY5LXxVh3A+ZNfMX6Lo0id3f3tKj/ottadevRJluNjBzOV+9avXVSfAanm4/YM1aSbRqF1cNul8E6y2/zAGmcZaKqXBdOqybgsTdd7ZEQQHV+XF6AaWpmyiaqjFx1lGMMimDmQ/boYE+Z9TwDrHt6p4UsgcCLrcKmrKQyg4fF04Ez2j1n4L9NAM3gvVaEX5bqxMSh04wzUlfl1SnL2Xoj931tiovTiq5omZjHgOOMDKxmOaNbCZizs/fG7cvStfQfBry4QttTSV+FHZQn2wW6/r6x+suhpcKcbXtj8w9NVOZZ

System Info

No response

Any additional comments?

No response

avatar
Mar 30th 2023

@vue/reactivity low-level effect() is synchronous unless you provide a scheduler.

You want to be using watchEffect from the vue package

avatar
Mar 30th 2023

The [...b] and b.push() inside the effect will be registering different dependencies on b. Both of these are being triggered by the external b.push(), so they each trigger the effect. The scheduler handles the deduplication when using watchEffect, but if you use effect directly you will get the same effect being triggered twice by that single push().