0
0
Svelteframework~20 mins

onDestroy in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte onDestroy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when onDestroy runs in a Svelte component?

Consider a Svelte component that uses onDestroy to clean up resources. What is the expected behavior when the component is removed from the page?

Svelte
import { onDestroy } from 'svelte';

let timer;
onDestroy(() => {
  clearInterval(timer);
  console.log('Cleanup done');
});

timer = setInterval(() => console.log('Tick'), 1000);
AThe interval stops and 'Cleanup done' is logged when the component is removed.
BThe interval continues running even after the component is removed.
C'Cleanup done' is logged immediately when the component is created.
DThe interval is cleared but 'Cleanup done' is never logged.
Attempts:
2 left
💡 Hint

Think about when onDestroy callbacks run in the component lifecycle.

state_output
intermediate
2:00remaining
What is logged after the component unmounts?

Given this Svelte component code, what will be logged to the console after the component is removed?

Svelte
import { onDestroy } from 'svelte';

let count = 0;
const interval = setInterval(() => {
  count += 1;
  console.log(count);
}, 500);

onDestroy(() => {
  clearInterval(interval);
  console.log('Stopped at', count);
});
ANumbers counting up every 500ms, then 'Stopped at' with the last count when removed.
BOnly 'Stopped at 0' is logged immediately on removal.
CNo logs appear because the interval is never started.
DThe count resets to 0 and logs 'Stopped at 0' after removal.
Attempts:
2 left
💡 Hint

Consider how intervals and onDestroy interact over time.

📝 Syntax
advanced
2:00remaining
Which option correctly uses onDestroy in Svelte?

Choose the code snippet that correctly registers an onDestroy callback in a Svelte component.

A
import { onDestroy } from 'svelte';
function onDestroy() { console.log('Cleanup'); }
B
import { onDestroy } from 'svelte';
onDestroy = () => { console.log('Cleanup'); };
C
import { onDestroy } from 'svelte';
onDestroy(() => { console.log('Cleanup'); });
D
import { onDestroy } from 'svelte';
onDestroy.add(() => { console.log('Cleanup'); });
Attempts:
2 left
💡 Hint

Remember how onDestroy is used as a function to register callbacks.

🔧 Debug
advanced
2:00remaining
Why does this onDestroy callback not run?

Look at this Svelte component code. Why does the onDestroy callback never execute?

Svelte
import { onDestroy } from 'svelte';

function cleanup() {
  console.log('Cleaning up');
}

// Missing onDestroy call
cleanup();
ABecause the component never mounts, so <code>onDestroy</code> is skipped.
BBecause <code>cleanup</code> is called immediately and not on destroy.
CBecause <code>onDestroy</code> only works with async functions.
DBecause <code>onDestroy</code> was never called to register the cleanup function.
Attempts:
2 left
💡 Hint

Check how onDestroy is supposed to be used to register callbacks.

🧠 Conceptual
expert
3:00remaining
How does onDestroy behave with nested components in Svelte?

Consider a parent Svelte component that includes a child component. Both use onDestroy callbacks. When the parent is removed from the DOM, which onDestroy callbacks run and in what order?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  import { onDestroy } from 'svelte';
  onDestroy(() => console.log('Parent destroyed'));
</script>
<Child />

<!-- Child.svelte -->
<script>
  import { onDestroy } from 'svelte';
  onDestroy(() => console.log('Child destroyed'));
</script>
AOnly the child's <code>onDestroy</code> runs because the parent is not destroyed directly.
BFirst the child's <code>onDestroy</code> runs, then the parent's <code>onDestroy</code> runs.
COnly the parent's <code>onDestroy</code> runs because the child is removed with it.
DBoth run simultaneously with no guaranteed order.
Attempts:
2 left
💡 Hint

Think about how nested components unmount in Svelte.