Vue 3 Composition API Cheatsheet

Complete Vue 3 Composition API reference: reactivity, computed, watchers, lifecycle hooks.

APIDescriptionSyntaxExampleCategory
ref()Create reactive referenceconst count = ref(0)const count = ref(0); count.value++Reactivity
reactive()Create reactive objectconst state = reactive({ count: 0 })const state = reactive({ count: 0, name: "Vue" })Reactivity
computed()Create computed propertyconst double = computed(() => count.value * 2)const fullName = computed(() => first + " " + last)Computed
watch()Watch for reactive changeswatch(source, (newVal, oldVal) => {})watch(count, (newVal) => console.log(newVal))Watchers
watchEffect()Auto-track dependencieswatchEffect(() => { ... })watchEffect(() => console.log(count.value))Watchers
onMounted()Run on component mountonMounted(() => { ... })onMounted(() => { fetchData() })Lifecycle
onUpdated()Run after updateonUpdated(() => { ... })onUpdated(() => { console.log("Updated") })Lifecycle
onUnmounted()Run before unmountonUnmounted(() => { ... })onUnmounted(() => { cleanup() })Lifecycle
provide()Provide value to childrenprovide(key, value)provide("theme", "dark")Templates
inject()Inject provided valueconst value = inject(key, defaultValue)const theme = inject("theme", "light")Templates