Lesson 24 — watchEffect
Effects that track themselves
watchEffect(fn) runs fn immediately and records every reactive value fn reads. Whenever any of them changes, it runs fn again. Unlike watch, you do not list the sources — Vue discovers them automatically.
watchEffect(() => {
document.title = cartCount.value + " items" // re-runs when cartCount changes
})
Why it matters: syncing to the page title, localStorage or a log stays correct without listing dependencies by hand.
Use watchEffect to log "count is" plus the value. Open the console: on load it shows count is 0, and each click adds count is 1, count is 2…
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Vue.js Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Vue.js Foundations books — check the publication year so the examples match current versions.
Search Amazon →