Skip to content

watchEffect performs bad with emits #13338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FanetheDivine opened this issue May 16, 2025 · 2 comments · May be fixed by #6688
Open

watchEffect performs bad with emits #13338

FanetheDivine opened this issue May 16, 2025 · 2 comments · May be fixed by #6688
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem

Comments

@FanetheDivine
Copy link

Vue version

3.5.13

Link to minimal reproduction

https://codesandbox.io/p/devbox/vue-test-54xk9s

Steps to reproduce

run the project and open devtool

What is expected?

The number on page is 1. 1 second later it turns to 2 and doesn't change.
An "setup" and an "effect" are printed

What is actually happening?

The number on page is 1. 1 second later it turns to 3, and every 1 second later it grows 2
An "setup" and an "effect", and every 1 second later an "effect" is printed

System Info

Any additional comments?

it seems like emits were collected as watchEffect's dependence

@edison1105
Copy link
Member

a workaround

const stop = watchEffect(() => {
  console.log("effect");
  emits("event");
  setTimeout(() => {
    emits("event");
    stop()
  }, 1000);
});

@edison1105 edison1105 added has workaround A workaround has been found to avoid the problem 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels May 16, 2025
@teleskop150750
Copy link

The problem is that during the increment the trigger of the reactive variable is called

vuejs/rfcs#747

workaround

<script setup>
import Child from './Child.vue'
import {ref} from 'vue'

let n = 0
const num = ref(n)
const foo = () => {
    n += 1
    num.value = n 
}
</script>
<template>
    <div>{{num}}
        <Child @event="foo"></Child>
    </div>
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem
Projects
None yet
3 participants