Bird
Raised Fist0
Vueframework~8 mins

Directive hooks (mounted, updated, unmounted) in Vue - Performance & Optimization

Choose your learning style10 modes available
Performance: Directive hooks (mounted, updated, unmounted)
MEDIUM IMPACT
Directive hooks affect how and when DOM updates happen, impacting rendering speed and responsiveness.
Using directive hooks to update DOM elements on data changes
Vue
mounted(el) { el.style.color = 'red'; } updated(el, binding) { if (binding.oldValue !== binding.value) { el.style.color = binding.value; } }
Only updates style when the value changes, avoiding redundant DOM writes.
📈 Performance Gainreduces reflows to only when necessary, improving INP
Using directive hooks to update DOM elements on data changes
Vue
mounted(el) { el.style.color = 'red'; } updated(el) { el.style.color = 'red'; }
Repeatedly setting the same style in updated hook triggers unnecessary style recalculations and reflows.
📉 Performance Costtriggers 1 reflow per update even if style is unchanged
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Unconditional style update in updated hookMultiple writes per updateTriggers reflow every updateHigh paint cost[X] Bad
Conditional style update checking value changeWrites only on changeReflow only when neededLower paint cost[OK] Good
Rendering Pipeline
Directive hooks run during component lifecycle and can trigger style recalculations and layout if they modify the DOM.
→Style Calculation
→Layout
→Paint
⚠️ BottleneckLayout (reflow) caused by DOM style changes in hooks
Core Web Vital Affected
INP
Directive hooks affect how and when DOM updates happen, impacting rendering speed and responsiveness.
Optimization Tips
1Avoid unconditional DOM writes in updated directive hooks.
2Use mounted hook for initial DOM setup only.
3Compare old and new values before updating DOM in updated hook.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of updating DOM styles unconditionally in the updated directive hook?
AIncreases bundle size significantly
BTriggers unnecessary reflows and slows interaction responsiveness
CBlocks initial page load
DCauses layout shifts during page load
DevTools: Performance
How to check: Record a performance profile while interacting with the component using directives. Look for frequent style recalculations and layout thrashing.
What to look for: High number of Layout events and long tasks during updates indicate inefficient directive hooks.

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