0
0
Vueframework~20 mins

onMounted and onUnmounted 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
2:00remaining
What happens when a Vue component using onMounted runs?

Consider a Vue 3 component that uses onMounted to log a message. What will you see in the browser console when the component first appears?

Vue
import { onMounted } from 'vue';

export default {
  setup() {
    onMounted(() => {
      console.log('Component is now mounted');
    });
  }
}
AThe message 'Component is now mounted' appears once in the console when the component is added to the page.
BThe message appears repeatedly every second after the component mounts.
CThe message appears before the component is rendered on the page.
DNo message appears because onMounted only works with class components.
Attempts:
2 left
💡 Hint

Think about when onMounted runs in the component lifecycle.

component_behavior
intermediate
2:00remaining
What is the effect of onUnmounted in a Vue component?

In a Vue 3 component, onUnmounted is used to clean up resources. What happens when the component is removed from the page?

Vue
import { onUnmounted } from 'vue';

export default {
  setup() {
    onUnmounted(() => {
      console.log('Component is now unmounted');
    });
  }
}
AThe message appears when the component is first created.
BThe message appears every time the component updates.
CThe message 'Component is now unmounted' appears once in the console when the component is removed from the page.
DNo message appears because onUnmounted only works with class components.
Attempts:
2 left
💡 Hint

Remember when onUnmounted runs in the component lifecycle.

state_output
advanced
2:00remaining
What is the console output when using both onMounted and onUnmounted in a Vue component?

Given this Vue 3 component, what will the console show when the component is mounted and then unmounted?

Vue
import { ref, onMounted, onUnmounted } from 'vue';

export default {
  setup() {
    const count = ref(0);

    onMounted(() => {
      console.log('Mounted with count:', count.value);
    });

    onUnmounted(() => {
      console.log('Unmounted with count:', count.value);
    });

    return { count };
  }
}
AConsole logs 'Mounted with count: 0' when mounted and 'Unmounted with count: 0' when unmounted.
BConsole logs 'Mounted with count: 0' twice and no unmounted message.
CConsole logs only 'Unmounted with count: 0' when unmounted.
DNo console output because count is not updated.
Attempts:
2 left
💡 Hint

Think about when each lifecycle hook runs and the initial value of count.

🔧 Debug
advanced
2:00remaining
Why does this Vue component's onUnmounted callback never run?

Look at this Vue 3 component. Why does the message inside onUnmounted never appear in the console?

Vue
import { onUnmounted } from 'vue';

export default {
  setup() {
    onUnmounted(() => {
      console.log('Component unmounted');
    });
  },
  template: '<div>Test</div>'
};
AThe component is missing a <code>name</code> property, so hooks don't run.
B<code>onUnmounted</code> must be used inside <code>mounted()</code> hook to work.
CThe console.log is inside the wrong lifecycle hook; it should be <code>onBeforeUnmount</code>.
DThe component is never removed from the DOM, so <code>onUnmounted</code> never runs.
Attempts:
2 left
💡 Hint

Think about when onUnmounted runs and what triggers it.

🧠 Conceptual
expert
3:00remaining
How do onMounted and onUnmounted help manage side effects in Vue 3?

Which statement best explains the role of onMounted and onUnmounted in managing side effects like timers or event listeners?

A<code>onMounted</code> and <code>onUnmounted</code> only run once when the app starts and stops, not per component.
B<code>onMounted</code> sets up side effects after the component appears, and <code>onUnmounted</code> cleans them up when it disappears, preventing memory leaks.
C<code>onMounted</code> and <code>onUnmounted</code> are used only for updating the component's template, not for side effects.
D<code>onMounted</code> cleans up side effects before the component appears, and <code>onUnmounted</code> sets them up after it disappears.
Attempts:
2 left
💡 Hint

Think about how to avoid leftover timers or listeners after a component is gone.