0Pricing
Sveltejs Academy · Lesson

$derived.by for Complex Derivations

Compute derived values with arbitrary logic using the callback form of $derived.

Multi-Statement Derivations

For derived values needing more than one expression, use the callback form.

let result = $derived.by(() => {
  if (count > 10) return "high";
  if (count > 5) return "medium";
  return "low";
});

Same Reactivity

The callback tracks dependencies the same way as a single-expression $derived.

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