watchEffect() Run an Effect with Automatic Tracking

Last updated: 2026-10-22

watchEffect() Run an Effect with Automatic Tracking

watchEffect() runs the function immediately and tracks whatever reactive data it reads — re-running whenever a dependency changes.

GroupReactivity: Core
KindAPI
ScopeComponent
Vue Version3.0+
SSR SupportYes

📝 Syntax

watchEffect(() => {
  document.title = `${count.value} clicks`  // tracks count automatically
})

// cleanup support
watchEffect((onCleanup) => { const t = setTimeout(fn, 500); onCleanup(() => clearTimeout(t)) })

⚙️ Arguments

effectThe effect function; receives onCleanup to register teardown
optionsflush (pre|post|sync), onTrack and onTrigger

Returns:A stop function that cancels the effect

▶ Example

Edit the code below and see the live result:

Output:

The title becomes '0 clicks' on mount and updates after every click.

💡 Key Points

💡 Tip No dependency list to maintain: whatever it reads is tracked.
ℹ️ Note No old value is provided — use watch() when you need before/after comparison.
⚠️ Warning Cancel stale requests via onCleanup to avoid race conditions on rapid changes.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q watchEffect or watch?
A Simple dependencies and no old value → watchEffect. Old values, precise sources or laziness → watch.
Q Why does it re-run for data I did not use?
A Only what is actually read is tracked — dependencies follow the branch taken.
Q Can watchEffect be stopped?
A Yes — call the returned stop(); it also stops automatically on unmount.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏