Bird
Raised Fist0
Vueframework~10 mins

Directive hooks (mounted, updated, unmounted) in Vue - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Directive hooks (mounted, updated, unmounted)
Directive Created
↓
mounted hook runs
↓
Element inserted in DOM
↓
Reactive data changes?
Yes↓
updated hook runs
↓
Element updated in DOM
↓
Directive removed?
Yes↓
unmounted hook runs
↓
Cleanup done
This flow shows how Vue directive hooks run: mounted when element is added, updated when reactive data changes, and unmounted when element is removed.
Execution Sample
Vue
const myDirective = {
  mounted(el) { el.textContent = 'Hello'; },
  updated(el) { el.textContent = 'Updated'; },
  unmounted(el) { console.log('Cleaned'); }
};
A Vue directive that sets text on mount, updates text on reactive changes, and logs cleanup on unmount.
Execution Table
StepHook TriggerElement Text ContentConsole OutputDescription
1mounted"Hello"Directive mounted, sets text to 'Hello'
2Reactive data changes"Updated"updated hook runs, text changes to 'Updated'
3Reactive data changes"Updated"updated hook runs again, text remains 'Updated'
4Directive removed"Updated"'Cleaned'unmounted hook runs, logs 'Cleaned'
💡 Directive lifecycle ends after unmounted hook runs and cleanup is done.
Variable Tracker
VariableStartAfter mountedAfter updated 1After updated 2After unmounted
el.textContent"""Hello""Updated""Updated""Updated"
console.log"""""""""Cleaned"
Key Moments - 3 Insights
Why does the 'updated' hook run multiple times?
Because reactive data changes multiple times, triggering the updated hook each time as shown in rows 2 and 3 of the execution_table.
Does the 'mounted' hook run again after updates?
No, the mounted hook runs only once when the directive is first attached, as shown in step 1 of the execution_table.
What happens in the 'unmounted' hook?
It runs when the directive is removed from the DOM to clean up, here it logs 'Cleaned' as shown in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the element's text content after the first updated hook runs?
A"Updated"
B"Cleaned"
C"Hello"
D""
💡 Hint
Check row 2 under 'Element Text Content' in the execution_table.
At which step does the unmounted hook run?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look for 'Directive removed' and 'unmounted hook runs' in the execution_table.
If the reactive data never changes, which hooks run?
Amounted and updated
Bonly mounted
Cmounted and unmounted
Donly unmounted
💡 Hint
Refer to the concept_flow showing mounted runs on insert and unmounted on removal, updated runs only on reactive changes.
Concept Snapshot
Vue directive hooks:
- mounted: runs once when element is inserted
- updated: runs on reactive data changes
- unmounted: runs when element is removed
Use hooks to manage element behavior and cleanup.
Full Transcript
This visual trace shows Vue directive hooks lifecycle. First, the mounted hook runs when the directive attaches to the element, setting its text content. Then, when reactive data changes, the updated hook runs multiple times, updating the text content each time. Finally, when the directive is removed from the DOM, the unmounted hook runs to clean up, here logging a message. The variable tracker shows how the element's text content changes step by step and when console logs occur. Key moments clarify why updated runs multiple times and that mounted runs only once. The quiz tests understanding of hook timing and effects.

Practice

(1/5)
1. What does the mounted hook in a Vue directive do?
easy
A. Runs every time the component updates
B. Runs once when the directive is attached to the element
C. Runs when the directive is removed from the element
D. Runs only when the element is clicked

Solution

  1. Step 1: Understand the purpose of mounted hook

    The mounted hook runs once when the directive is first attached to the element in the DOM.
  2. Step 2: Compare with other hooks

    The updated hook runs on data or element changes, and unmounted runs when the directive is removed, so they do not match the description of mounted.
  3. Final Answer:

    Runs once when the directive is attached to the element -> Option B
  4. Quick Check:

    mounted = runs once on attach [OK]
Hint: Remember: mounted runs once when directive starts [OK]
Common Mistakes:
  • Confusing mounted with updated hook
  • Thinking mounted runs multiple times
  • Mixing unmounted with mounted
2. Which of the following is the correct syntax to define the unmounted hook in a Vue directive?
easy
A. onUnmounted(el, binding) { /* code */ }
B. unmount(el, binding) { /* code */ }
C. destroyed(el, binding) { /* code */ }
D. unmounted(el, binding) { /* code */ }

Solution

  1. Step 1: Recall the correct hook name

    The correct hook name for cleanup when a directive is removed is unmounted.
  2. Step 2: Check syntax correctness

    The hook is defined as a function with parameters el and binding, so unmounted(el, binding) { /* code */ } is correct.
  3. Final Answer:

    unmounted(el, binding) { /* code */ } -> Option D
  4. Quick Check:

    Correct hook name is unmounted [OK]
Hint: Use exact hook names: unmounted, not unmount or destroyed [OK]
Common Mistakes:
  • Using 'unmount' instead of 'unmounted'
  • Confusing with lifecycle hooks like 'destroyed'
  • Adding 'on' prefix incorrectly
3. Given this directive code, what will be logged when the bound element's text changes?
const myDirective = {
  mounted(el) {
    console.log('Mounted')
  },
  updated(el) {
    console.log('Updated')
  },
  unmounted(el) {
    console.log('Unmounted')
  }
}
medium
A. 'Mounted' then 'Updated' will log on text change
B. No logs will appear
C. 'Unmounted' will log on text change
D. Only 'Mounted' will log once

Solution

  1. Step 1: Understand when each hook runs

    mounted runs once when directive attaches, updated runs on element or data changes, unmounted runs when directive is removed.
  2. Step 2: Analyze the scenario

    When the element's text changes, the updated hook runs, so both Mounted (once) and Updated (on change) will log.
  3. Final Answer:

    'Mounted' then 'Updated' will log on text change -> Option A
  4. Quick Check:

    mounted once, updated on change [OK]
Hint: Updated logs on changes, mounted only once [OK]
Common Mistakes:
  • Thinking updated runs only once
  • Expecting unmounted to run on text change
  • Ignoring mounted logs
4. Identify the error in this directive definition:
const directive = {
  mounted(el) {
    console.log('Mounted')
  },
  updated() {
    console.log('Updated')
  },
  unmounted(el) {
    console.log('Unmounted')
  }
}
medium
A. The mounted hook should not have parameters
B. The unmounted hook should be named 'destroyed'
C. The updated hook is missing the 'el' parameter
D. There is no error in this code

Solution

  1. Step 1: Check parameters for each hook

    Directive hooks receive the element as the first parameter. The updated hook is missing the el parameter.
  2. Step 2: Verify other hooks

    mounted and unmounted correctly have el parameter. The unmounted hook is correctly named.
  3. Final Answer:

    The updated hook is missing the 'el' parameter -> Option C
  4. Quick Check:

    All hooks need 'el' parameter [OK]
Hint: All directive hooks need 'el' parameter [OK]
Common Mistakes:
  • Omitting parameters in hooks
  • Confusing lifecycle hook names
  • Assuming no parameters needed
5. You want to create a directive that adds a click event listener when attached and removes it when the directive is removed. Which hooks should you use to add and remove the listener?
hard
A. Add listener in mounted, remove in unmounted
B. Add listener in updated, remove in mounted
C. Add listener in unmounted, remove in updated
D. Add listener in updated, remove in updated

Solution

  1. Step 1: Determine when to add event listener

    The listener should be added once when the directive attaches, so use mounted.
  2. Step 2: Determine when to remove event listener

    Cleanup should happen when the directive is removed, so use unmounted to remove the listener.
  3. Final Answer:

    Add listener in mounted, remove in unmounted -> Option A
  4. Quick Check:

    mounted = add, unmounted = cleanup [OK]
Hint: Add in mounted, clean up in unmounted [OK]
Common Mistakes:
  • Adding/removing listeners in updated hook
  • Removing listener before adding it
  • Using unmounted to add listener
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard