Complete the code to import the lifecycle function that runs cleanup code in a Svelte component.
import { [1] } from 'svelte';
The onDestroy function is imported from 'svelte' to run cleanup code when a component is removed.
Complete the code to register a cleanup function inside the onDestroy lifecycle in Svelte.
onDestroy(() => { [1] });The function inside onDestroy runs when the component is removed, so logging 'Component destroyed' is appropriate.
Fix the error in the code by completing the onDestroy usage correctly.
onDestroy [1] console.log('Cleanup'); });
The correct syntax for the function passed to onDestroy is an arrow function with curly braces: () => { ... }.
Fill both blanks to correctly import and use onDestroy to clear an interval timer.
import { [1] } from 'svelte'; const timer = setInterval(() => console.log('Tick'), 1000); [2](() => { clearInterval(timer); });
You import onDestroy and use it to clear the interval when the component is destroyed, preventing memory leaks.
Fill all three blanks to use onDestroy to unsubscribe from a store in Svelte.
import { [1] } from 'svelte'; const unsubscribe = store.subscribe(value => { console.log(value); }); [2](() => { [3](); });
You import onDestroy to run cleanup code, call it to register the cleanup function, and inside that function call unsubscribe() to stop listening to the store.