Menu

Vue v-for: The Directive Unmounted Hook Bug, Fixed in 3.5.41
๐Ÿ“ฐ

Vue v-for: The Directive Unmounted Hook Bug, Fixed in 3.5.41

DEV Community: tutorialยทParsa Jยท2 days ago
#jZ8BH44k
#devtotutorial#devto#dev#directive#element
Reading 0:00
15s threshold

You write a custom directive once โ€” v-tooltip , v-click-outside , v-ripple โ€” attach something in mounted() , tear it down in unmounted() , and trust the pair like you'd trust addEventListener / removeEventListener . Then you drop it onto a row inside a filterable v-for list. Filter an item out. No error. No warning. The row disappears from the screen โ€” and on some Vue 3 versions, unmounted() never ran. The listener it attached is still live, attached to a DOM node nothing references anymore. This was a real bug in Vue's own compiler, open for years, fixed only in Vue 3.5.41 โ€” and understanding why it happened teaches you more about how v-for actually works than the fix note ever will.

What You'll Learn

By the end of this article you'll be able to:

Explain why a custom directive's lifecycle hooks can be skipped for an element inside v-for , even though the same directive works fine outside a loop

Reproduce the symptom, confirm whether your installed Vue version is affected, and verify the fix

Describeโ€ฆ

Continue reading โ€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.