0
0
Vueframework~20 mins

Effective scope for cleanup in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Cleanup Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens to the cleanup function in this Vue 3 component?

Consider this Vue 3 component using the Composition API. What will happen when the component unmounts?

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

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

    onMounted(() => {
      const interval = setInterval(() => {
        count.value++;
      }, 1000);

      onUnmounted(() => {
        clearInterval(interval);
      });
    });

    return { count };
  }
}
AThe component will throw an error because onUnmounted cannot be called inside onMounted.
BThe interval will be cleared properly when the component unmounts, stopping the count increment.
CThe interval will never be cleared because onUnmounted is nested inside onMounted, so cleanup won't run.
DThe count will reset to zero when the component unmounts due to the cleanup function.
Attempts:
2 left
💡 Hint

Think about when lifecycle hooks run and if nesting affects their execution.

lifecycle
intermediate
2:00remaining
When is the cleanup function called in Vue 3's watchEffect?

In Vue 3, watchEffect can return a cleanup function. When exactly is this cleanup function executed?

Vue
import { ref, watchEffect } from 'vue';

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

    watchEffect((onCleanup) => {
      console.log('Effect runs with count:', count.value);

      onCleanup(() => {
        console.log('Cleanup for count:', count.value);
      });
    });

    return { count };
  }
}
AThe cleanup function runs after the effect runs each time.
BThe cleanup function runs only once when the component unmounts.
CThe cleanup function runs before the effect re-runs and when the component unmounts.
DThe cleanup function runs only if the watched value changes to null.
Attempts:
2 left
💡 Hint

Think about how watchEffect manages reactive dependencies and cleanup.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 cleanup function not run on unmount?

Examine the code below. Why does the cleanup function not execute when the component unmounts?

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

export default {
  setup() {
    const timer = ref(null);

    onMounted(() => {
      timer.value = setTimeout(() => {
        console.log('Timer done');
      }, 1000);
    });

    onUnmounted(() => {
      clearTimeout(timer.value);
    });

    return {};
  }
}
ABecause setTimeout inside onMounted delays the cleanup registration until after unmount.
BBecause onUnmounted is not imported from 'vue', so the hook never runs.
CBecause the timer variable is declared with ref, which is not reactive enough for cleanup.
DBecause clearTimeout is called with a ref object instead of the timer ID, so the timeout is not cleared.
Attempts:
2 left
💡 Hint

Check the argument passed to clearTimeout carefully.

📝 Syntax
advanced
2:00remaining
Which option correctly registers a cleanup function in Vue 3's onMounted hook?

Choose the code snippet that correctly registers a cleanup function to run when the component unmounts.

A
onMounted(() => {
  const id = setInterval(() => {}, 1000);
  onUnmounted(() => clearInterval(id));
});
B
onMounted(() => {
  const id = setInterval(() => {}, 1000);
  return () => clearInterval(id);
});
C
onMounted(() => {
  const id = setInterval(() => {}, 1000);
  onCleanup(() => clearInterval(id));
});
D
onMounted(() => {
  const id = setInterval(() => {}, 1000);
  cleanup(() => clearInterval(id));
});
Attempts:
2 left
💡 Hint

Remember the correct lifecycle hook for cleanup in Vue 3.

🧠 Conceptual
expert
3:00remaining
Why is it important to scope cleanup functions inside the setup function in Vue 3?

In Vue 3's Composition API, why should cleanup functions be registered inside the setup() function rather than outside it?

ABecause cleanup functions registered inside <code>setup()</code> are tied to the component instance lifecycle, ensuring proper cleanup on unmount.
BBecause registering cleanup outside <code>setup()</code> causes Vue to run cleanup multiple times per component instance.
CBecause Vue only allows cleanup functions inside <code>setup()</code> to access reactive state variables.
DBecause cleanup functions outside <code>setup()</code> run immediately when the component mounts, not on unmount.
Attempts:
2 left
💡 Hint

Think about how Vue tracks component lifecycles and reactive scopes.