$effect.root for Manual Cleanup
Create an effect root to control when effects are started and stopped.
Effects Outside Components
$effect only works inside component lifecycles. For app-wide effects, use $effect.root.
Create a Root
$effect.root returns a teardown function.
const teardown = $effect.root(() => {
$effect(() => { /* ... */ });
});All lessons in this course
- $state.raw for Non-Reactive State
- $derived.by for Complex Derivations
- $effect.root for Manual Cleanup
- Migrating from Svelte 4 to 5