← All posts

Vue 3 Composition API: Practical Tips

May 5, 2026#Vue161 words · 1 min read阅读中文原文 ↗

The short version

ref, reactive, computed — which one to pick, in one read.

TestedVue

ref vs reactive

One line: prefer ref.

import { ref, reactive } from 'vue'

// ✅ Recommended: ref handles both primitives and objects
const count = ref(0)
const user = ref({ name: 'Alice', age: 18 })

// ⚠️ Only reach for reactive when you know it will only ever hold an object
const state = reactive({ count: 0 })

reactive has two traps:

  1. You can’t destructure it (destructuring loses reactivity)
  2. You can’t replace it wholesale (only assign key by key)

computed is cached

As long as the dependencies don’t change, reading it repeatedly computes once:

const fullName = computed(() => `${first.value} ${last.value}`)

console.log(fullName.value) // computes
console.log(fullName.value) // cache hit

Three ways to write watch

// watch a ref
watch(count, (n, o) => console.log(n, o))

// watch a getter
watch(() => user.value.age, (n) => console.log(n))

// run immediately + deep
watch(source, cb, { immediate: true, deep: true })

Remember: watchEffect collects dependencies for you, but it’s harder to debug. Use it sparingly.

Was this useful?

If this post helped, you can buy me a coffee ☕