Advanced Runes ($state.raw, $derived.by, $inspect) in Svelte

From the Svelte cheat sheet · Runes (Svelte 5) · verified Jul 2026

Advanced Runes ($state.raw, $derived.by, $inspect)

Opt out of deep reactivity, derive with a function, debug reactive values

svelte
<script>
  // $state.raw — skip deep proxy wrapping (huge perf win for big arrays/objects)
  let rows = $state.raw([])
  // Must replace the whole reference; mutating in place won't trigger updates:
  rows = [...rows, newRow]    // ✓
  // rows.push(newRow)         // ✗ — no reactivity

  // $derived.by — function form for multi-statement derivations
  let stats = $derived.by(() => {
    const total = rows.length
    const active = rows.filter(r => r.active).length
    return { total, active, ratio: total ? active / total : 0 }
  })

  // $inspect — log a reactive value (dev-only)
  $inspect(rows)
  $inspect(stats).with((type, value) => {
    if (type === 'update') console.log('stats changed', value)
  })
</script>
💡 $state.raw skips the Proxy — big perf win when you only swap references
📌 Use $derived.by for multi-statement derivations; $derived for one-liners
⚡ $inspect is stripped at build time — leave it in dev code freely
🎯 $state.snapshot(x) gives you a plain object for fetch/JSON.stringify
runessvelte5reactivitydebugging

More Svelte tasks

Back to the full Svelte cheat sheet