Overview - onDestroy
What is it?
onDestroy is a special function in Svelte that runs when a component is about to be removed from the page. It lets you clean up things like timers, event listeners, or subscriptions to avoid problems later. Think of it as a way to tidy up before the component disappears. This helps keep your app fast and bug-free.
Why it matters
Without onDestroy, leftover tasks like running timers or active event listeners could keep using memory and slow down your app. This can cause bugs or crashes, especially in bigger apps where many components come and go. onDestroy solves this by giving you a clear place to stop or remove these tasks, making your app smoother and more reliable.
Where it fits
Before learning onDestroy, you should understand basic Svelte components and lifecycle concepts like onMount. After onDestroy, you can explore more advanced lifecycle hooks and state management techniques to build complex, efficient apps.