0Pricing
Sveltejs Academy · Lesson

$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

  1. $state.raw for Non-Reactive State
  2. $derived.by for Complex Derivations
  3. $effect.root for Manual Cleanup
  4. Migrating from Svelte 4 to 5
← Back to Sveltejs Academy