What if you could automate element behavior exactly when it appears or disappears, without messy manual code?
Why Directive hooks (mounted, updated, unmounted) in Vue? - Purpose & Use Cases
Imagine you want to add special effects to HTML elements when they appear, change, or disappear on your page. You try to do this by manually finding elements and adding event listeners or styles every time something changes.
Manually tracking elements is tiring and error-prone. You might forget to clean up listeners, causing bugs or slowdowns. Also, updating effects when data changes means writing repetitive code that's hard to maintain.
Vue's directive hooks like mounted, updated, and unmounted let you attach code that runs exactly when an element appears, updates, or is removed. This keeps your code clean, automatic, and reliable.
const el = document.querySelector('#myEl'); el.style.color = 'red'; el.addEventListener('click', () => alert('Clicked!'));
const myDirective = {
mounted(el) { el.style.color = 'red'; el.onclick = () => alert('Clicked!'); },
unmounted(el) { el.onclick = null; }
};You can easily add, update, and clean up element behaviors tied to their lifecycle without messy manual code.
Think of a tooltip that appears when you hover over a button and disappears when you move away. Directive hooks let you add and remove the tooltip cleanly as the button enters or leaves the page.
Manual element handling is slow and error-prone.
Directive hooks run code automatically at key element lifecycle moments.
This makes your UI code cleaner, safer, and easier to maintain.
Practice
mounted hook in a Vue directive do?Solution
Step 1: Understand the purpose of
Themountedhookmountedhook runs once when the directive is first attached to the element in the DOM.Step 2: Compare with other hooks
Theupdatedhook runs on data or element changes, andunmountedruns when the directive is removed, so they do not match the description ofmounted.Final Answer:
Runs once when the directive is attached to the element -> Option BQuick Check:
mounted = runs once on attach [OK]
- Confusing mounted with updated hook
- Thinking mounted runs multiple times
- Mixing unmounted with mounted
unmounted hook in a Vue directive?Solution
Step 1: Recall the correct hook name
The correct hook name for cleanup when a directive is removed isunmounted.Step 2: Check syntax correctness
The hook is defined as a function with parameterselandbinding, sounmounted(el, binding) { /* code */ }is correct.Final Answer:
unmounted(el, binding) { /* code */ } -> Option DQuick Check:
Correct hook name is unmounted [OK]
- Using 'unmount' instead of 'unmounted'
- Confusing with lifecycle hooks like 'destroyed'
- Adding 'on' prefix incorrectly
const myDirective = {
mounted(el) {
console.log('Mounted')
},
updated(el) {
console.log('Updated')
},
unmounted(el) {
console.log('Unmounted')
}
}Solution
Step 1: Understand when each hook runs
mountedruns once when directive attaches,updatedruns on element or data changes,unmountedruns when directive is removed.Step 2: Analyze the scenario
When the element's text changes, theupdatedhook runs, so bothMounted(once) andUpdated(on change) will log.Final Answer:
'Mounted' then 'Updated' will log on text change -> Option AQuick Check:
mounted once, updated on change [OK]
- Thinking updated runs only once
- Expecting unmounted to run on text change
- Ignoring mounted logs
const directive = {
mounted(el) {
console.log('Mounted')
},
updated() {
console.log('Updated')
},
unmounted(el) {
console.log('Unmounted')
}
}Solution
Step 1: Check parameters for each hook
Directive hooks receive the element as the first parameter. Theupdatedhook is missing theelparameter.Step 2: Verify other hooks
mountedandunmountedcorrectly haveelparameter. Theunmountedhook is correctly named.Final Answer:
The updated hook is missing the 'el' parameter -> Option CQuick Check:
All hooks need 'el' parameter [OK]
- Omitting parameters in hooks
- Confusing lifecycle hook names
- Assuming no parameters needed
Solution
Step 1: Determine when to add event listener
The listener should be added once when the directive attaches, so usemounted.Step 2: Determine when to remove event listener
Cleanup should happen when the directive is removed, so useunmountedto remove the listener.Final Answer:
Add listener in mounted, remove in unmounted -> Option AQuick Check:
mounted = add, unmounted = cleanup [OK]
- Adding/removing listeners in updated hook
- Removing listener before adding it
- Using unmounted to add listener
Ready to prove it?
Pass the test and this pattern moves unknown
