0Pricing
Sveltejs Academy · Lesson

$effect.root for Manual Cleanup

Create an effect root to control when effects are started and stopped.

$effect.root for Manual Cleanup is a free Sveltejs Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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(() => { /* ... */ });
});

Manual Lifecycle

You decide when the root effect stops; call the returned teardown to clean up.

Use Cases

Background tasks, global subscriptions, scripts that span beyond a single component.

Nested Effects

Inside a root, you can declare normal $effect calls.

Stores Replacement

$effect.root combined with $state can replace many store patterns at the module level.

Cleanup Composition

Returning cleanup functions from inner effects works as expected.

Test Setup

Useful in tests to run reactive code outside a component.

SSR Awareness

$effect.root respects SSR; effects do not run on the server.

Compared to onMount/onDestroy

$effect.root + teardown is the modern equivalent for cross-component effects.

Advanced API

Power users only — most app code uses $effect inside components.

Quick Check

When do you reach for $effect.root?

Recap

$effect.root runs effects outside component scope. The returned function tears them down on demand.

Frequently asked questions

Is the “$effect.root for Manual Cleanup” lesson free?

Yes — the full text of “$effect.root for Manual Cleanup” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “$effect.root for Manual Cleanup”?

Create an effect root to control when effects are started and stopped. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “$effect.root for Manual Cleanup” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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