Recall & Review
beginner
What is the purpose of the
onDestroy lifecycle function in Svelte?The
onDestroy function runs cleanup code when a component is removed from the page, like stopping timers or removing event listeners.Click to reveal answer
beginner
How do you import <code>onDestroy</code> in a Svelte component?You import it from 'svelte' like this: <code>import { onDestroy } from 'svelte';</code>Click to reveal answer
intermediate
Show a simple example of using
onDestroy to clear an interval timer.<pre>import { onDestroy } from 'svelte';
const timer = setInterval(() => {
console.log('Tick');
}, 1000);
onDestroy(() => {
clearInterval(timer);
});</pre>Click to reveal answer
beginner
When exactly does the
onDestroy callback run in the component lifecycle?It runs right before the component is removed from the DOM, allowing you to clean up resources.
Click to reveal answer
intermediate
Can you register multiple
onDestroy callbacks in one component?Yes, you can call
onDestroy multiple times to register several cleanup functions. All will run when the component is destroyed.Click to reveal answer
What is the main use of
onDestroy in Svelte?✗ Incorrect
onDestroy is used to run cleanup code when the component is removed from the DOM.
How do you use
onDestroy in a Svelte component?✗ Incorrect
You import onDestroy and call it with a function to run on component removal.
Which of these is a good use case for
onDestroy?✗ Incorrect
onDestroy is perfect for cleaning up things like timers.
Can you register more than one
onDestroy callback in a component?✗ Incorrect
Svelte allows multiple onDestroy callbacks; all run on destruction.
When does the
onDestroy callback run?✗ Incorrect
onDestroy runs just before the component is removed from the page.
Explain how and why you would use
onDestroy in a Svelte component.Think about what needs to be cleaned up when a component disappears.
You got /4 concepts.
Describe a scenario where forgetting to use
onDestroy could cause problems in a Svelte app.Imagine a timer keeps running even after you leave a page.
You got /4 concepts.