Sitemap
The 60-Second Programmer

Quick, actionable programming tips and insights delivered in just 60 seconds. Perfect for developers who want to learn on the go. Whether you’re a beginner or seasoned pro, our bite-sized lessons fit your schedule and boost your coding skills fast!

computed vs watch ⚡

Derive values with computed, react to changes with watch.

1 min read2 hours ago

--

Both respond to reactive changes, but they answer different questions. Mixing them up leads to clunky code.

computed — derive a new value

Use it when a value is calculated from other state. It’s cached and only re-runs when its dependencies change.

import { ref, computed } from 'vue'
const first = ref('Ada')
const last = ref('Lovelace')
const fullName = computed(() => `${first.value} ${last.value}`)
console.log(fullName.value) // "Ada Lovelace"

watch — run a side effect

Use it when a change should trigger an action — fetching data, logging, syncing to storage.

import { ref, watch } from 'vue'
const query = ref('')
watch(query, (newVal, oldVal) => {
console.log(`searching for ${newVal}`)
// fetchResults(newVal)
})

The rule of thumb

Returning a value for the template? → computed
Performing an action in response to a change? → watch

Get Marcelo Domingues’s stories in your inbox

Join Medium for free to get updates from this writer.

If you find yourself manually assigning to a ref inside a watch just to derive a value, you probably want computed instead.

TL;DR: computed produces a cached derived value; watch runs side effects when something changes.

Next time you reach for watch, ask “am I just calculating something?” — if so, computed is cleaner.

--

--

Written by Marcelo Domingues

🚀 Lead Software Engineer for Siemens | Crafting Code & Words | Empowering Tech Enthusiasts ✨ 📲 LinkedIn: https://www.linkedin.com/in/marcelogdomingues/

Published in The 60-Second Programmer

Quick, actionable programming tips and insights delivered in just 60 seconds. Perfect for developers who want to learn on the go. Whether you’re a beginner or seasoned pro, our bite-sized lessons fit your schedule and boost your coding skills fast!