Javascript is required
·
1 min read

Vue Tip: Lifecycle Hooks for Debugging

Vue Tip: Lifecycle Hooks for Debugging Image

Vue provides two lifecycle hooks that we can use for debugging purposes: onRenderTracked and onRenderTriggered.

Warning

These hooks are development-mode-only and not called during server-side rendering.

onRenderTracked

Registers a debug hook to be called when a reactive dependency has been tracked by the component's render effect:

1<script setup>
2import { ref, onRenderTracked } from 'vue'
3
4const count = ref(0)
5const count2 = ref(0)
6
7// Called twice, once for count and once for count2
8onRenderTracked((event) => {
9  console.log(event)
10})
11</script>

onRenderTriggered

Registers a debug hook to be called when a reactive dependency triggers the component's render effect to be re-run:

1<script setup>
2import { ref, onRenderTriggered } from 'vue'
3
4const count = ref(0)
5
6// Called when we update count
7onRenderTriggered((event) => {
8  console.log(event)
9})
10</script>

If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:

I will never share any of your personal data. You can unsubscribe at any time.