How does Vue 3's reactivity system work under the hood?
Vue 3 implements reactivity using JavaScript Proxies (ES6+). When you call reactive() or ref(), Vue wraps the value in a Proxy that intercepts all property access and mutations.
import { reactive, watchEffect } from 'vue'
const state = reactive({ count: 0 })
watchEffect(() => {
// Vue tracks that this effect reads state.count
console.log(state.count)
})
state.count++ // Vue triggers the effect → logs new value
// ref() wraps the value in an object with a .value getter/setter
const count = ref(0)
// Internally: { get value() { track(); return _value }, set value(v) { _value = v; trigger() } }
// reactive() wraps the object in a Proxy
const obj = reactive({ name: 'Alice' })
// Internally: new Proxy(obj, { get: track, set: trigger })
All Comments