0
0
Vueframework~20 mins

onBeforeMount and onBeforeUnmount in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
When does onBeforeMount run in a Vue component?
Consider a Vue 3 component using the Composition API. When exactly does the onBeforeMount lifecycle hook execute?
AWhen the component is unmounted from the DOM.
BRight after the component is mounted to the DOM.
CJust before the component is mounted to the DOM.
DWhen the component is created but before setup runs.
Attempts:
2 left
💡 Hint
Think about the name: 'before mount' means before the component appears in the page.
component_behavior
intermediate
1:30remaining
What happens when onBeforeUnmount is triggered?
In a Vue 3 component, what is the purpose of the onBeforeUnmount lifecycle hook?
AIt runs before the component is removed from the DOM.
BIt runs after the component is removed from the DOM.
CIt runs when the component is first created.
DIt runs when the component is updated.
Attempts:
2 left
💡 Hint
The name suggests it runs just before unmounting.
state_output
advanced
2:00remaining
What is the console output order for these hooks?
Given this Vue 3 component code, what is the order of console logs when the component mounts and then unmounts?
Vue
import { onBeforeMount, onBeforeUnmount } from 'vue';

export default {
  setup() {
    onBeforeMount(() => console.log('Before Mount'));
    onBeforeUnmount(() => console.log('Before Unmount'));
  }
}
A"Before Unmount" then "Before Mount"
BOnly "Before Mount" is logged
COnly "Before Unmount" is logged
D"Before Mount" then "Before Unmount"
Attempts:
2 left
💡 Hint
Think about the lifecycle: mount happens before unmount.
🔧 Debug
advanced
2:30remaining
Why does this onBeforeUnmount cleanup not work?
This Vue 3 component adds a window event listener but the cleanup in onBeforeUnmount does not remove it. Why?
import { onBeforeUnmount } from 'vue';

export default {
  setup() {
    function onResize() { console.log('resized'); }
    window.addEventListener('resize', onResize);

    onBeforeUnmount(() => {
      window.removeEventListener('resize', () => console.log('resized'));
    });
  }
}
Awindow.removeEventListener requires the event type to be uppercase.
BThe function passed to removeEventListener is a new anonymous function, not the original listener.
ConBeforeUnmount runs too late to remove event listeners.
DThe event listener was never added, so removal fails silently.
Attempts:
2 left
💡 Hint
Check if the function references match exactly.
🧠 Conceptual
expert
3:00remaining
How do onBeforeMount and onBeforeUnmount differ from onMounted and onUnmounted?
Choose the statement that best describes the difference between onBeforeMount/onBeforeUnmount and onMounted/onUnmounted hooks in Vue 3 Composition API.
A<code>onBeforeMount</code> and <code>onBeforeUnmount</code> run immediately before mounting and unmounting, while <code>onMounted</code> and <code>onUnmounted</code> run immediately after those events.
B<code>onBeforeMount</code> and <code>onBeforeUnmount</code> run only once, but <code>onMounted</code> and <code>onUnmounted</code> run multiple times during component updates.
C<code>onBeforeMount</code> and <code>onBeforeUnmount</code> are deprecated hooks replaced by <code>onMounted</code> and <code>onUnmounted</code>.
D<code>onBeforeMount</code> and <code>onBeforeUnmount</code> run only on server-side rendering, while <code>onMounted</code> and <code>onUnmounted</code> run on client-side.
Attempts:
2 left
💡 Hint
Focus on the timing of when these hooks run relative to mounting and unmounting.