Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/reactivity-debugging-hooks.md
1---2title: Use Debug Hooks to Trace Reactivity Issues3impact: MEDIUM4impactDescription: Debug hooks help identify which dependencies trigger re-renders and watcher executions5type: efficiency6tags: [vue3, reactivity, debugging, computed, watch, development]7---89# Use Debug Hooks to Trace Reactivity Issues1011**Impact: MEDIUM** - Vue provides debug hooks (`onTrack`, `onTrigger`, `renderTracked`, `renderTriggered`) that help identify exactly which reactive dependencies are being tracked and which mutations trigger re-execution. These are invaluable for debugging performance issues and unexpected re-renders.1213Debug hooks only work in development mode and are stripped in production builds. Use them to understand why a computed property, watcher, or component is re-executing.1415## Task Checklist1617- [ ] Use `onTrack` and `onTrigger` options on computed/watch for granular debugging18- [ ] Use `onRenderTracked` and `onRenderTriggered` lifecycle hooks for component render debugging19- [ ] Add `debugger` statements inside hooks to pause execution and inspect state20- [ ] Remove or comment out debug hooks before production (they're no-ops but add clutter)2122> **Note:** `onTrack` and `onTrigger` are development-only hooks. They are stripped from production builds and may not fire in test environments (e.g., Vitest, Jest) depending on how Vue is bundled. If you need to verify reactivity behavior in tests, use direct assertions on reactive state changes rather than relying on these debug callbacks.2324**Debugging computed properties:**25```javascript26import { ref, computed } from 'vue'2728const count = ref(0)29const doubled = computed(() => count.value * 2, {30onTrack(event) {31// Called when a dependency is tracked32// event.target = the reactive object33// event.key = the property being accessed34debugger35console.log('Tracking:', event)36},37onTrigger(event) {38// Called when a dependency mutation triggers re-computation39debugger40console.log('Triggered by:', event)41}42})43```4445**Debugging watchers:**46```javascript47import { ref, watch, watchEffect } from 'vue'4849const source = ref(0)5051// With watch()52watch(source, (newVal, oldVal) => {53console.log('Changed:', oldVal, '->', newVal)54}, {55onTrack(e) {56debugger // Pause to see what's being tracked57},58onTrigger(e) {59debugger // Pause to see what triggered the watcher60}61})6263// With watchEffect()64watchEffect(() => {65console.log('Source is:', source.value)66}, {67onTrack(e) {68console.log('Tracking dependency:', e.key)69},70onTrigger(e) {71console.log('Triggered by:', e.key, 'mutation')72}73})74```7576**Debugging component renders:**77```vue78<script setup>79import { onRenderTracked, onRenderTriggered, ref } from 'vue'8081const count = ref(0)8283// Called for every reactive dependency accessed during render84onRenderTracked((event) => {85console.log('Render tracked:', event.key, 'from', event.target)86debugger // Pause to inspect which dependencies are tracked87})8889// Called when a reactive dependency triggers re-render90onRenderTriggered((event) => {91console.log('Render triggered by:', event.key)92console.log('Old value:', event.oldValue)93console.log('New value:', event.newValue)94debugger // Pause to see exactly what caused the re-render95})96</script>97```9899**Options API equivalent:**100```javascript101export default {102data() {103return { count: 0 }104},105renderTracked(event) {106console.log('Dependency tracked during render:', event)107debugger108},109renderTriggered(event) {110console.log('Re-render triggered by:', event)111debugger112}113}114```115116**Debug event properties:**117```javascript118// The event object contains:119{120effect: ReactiveEffect, // The effect being debugged121target: object, // The reactive object122type: 'get' | 'set' | 'add' | 'delete' | 'clear',123key: string | symbol, // The property being accessed/mutated124oldValue: any, // Previous value (for onTrigger)125newValue: any // New value (for onTrigger)126}127```128129## Reference130- [Vue.js Reactivity in Depth - Debugging](https://vuejs.org/guide/extras/reactivity-in-depth.html#reactivity-debugging)131- [Vue.js computed() API](https://vuejs.org/api/reactivity-core.html#computed)132- [Vue.js onRenderTracked()](https://vuejs.org/api/composition-api-lifecycle.html#onrendertracked)133