All posts
sveltefrontend

Svelte 5 Runes, From Someone Who Shipped Svelte 4

Runes look like more typing. After a real project in Svelte 4, they read like the fix for every reactivity bug I had.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

My first real project was a reseller system in Svelte 4: landing page, WhatsApp OTP login, a dashboard for orders, invoices, and loyalty points. Svelte made it feel easy for about three weeks. Then the dashboard grew, and the magic started to bite.

Svelte 5 shipped this month with runes. The first reaction online was "this is just React hooks now." I don't think that's right, and the bugs I hit in Svelte 4 are the reason.

What Svelte 4 got wrong, quietly

In Svelte 4, any top-level let in a component is reactive, and $: reruns when its inputs change.

<script>
  let items = [];
  let filter = "";
  $: visible = items.filter((i) => i.name.includes(filter));
</script>

Lovely, until:

  • It only works at the top level of a .svelte file. Move that logic into a helper and the reactivity is gone. You reach for stores, which is a second system with its own rules.
  • Mutations don't count. items.push(x) does nothing until you write items = items. Every new teammate trips on this once.
  • $: ordering is implicit. Svelte sorts the statements by dependency, and when two of them touch the same variable, the result can surprise you.

None of these are dramatic. They're the kind of thing that costs an afternoon each, every few weeks.

Rendering diagram…

The same thing with runes

<script>
  let items = $state([]);
  let filter = $state("");
  let visible = $derived(items.filter((i) => i.name.includes(filter)));
</script>

More characters, yes. But now:

  • $state is deep. items.push(x) just works.
  • $derived says out loud that this value is computed. No guessing which $: is a side effect and which is a value.
  • Side effects get their own rune, $effect, so they stop hiding next to derived values.

The part that actually matters: it works outside components

Runes also work in .svelte.js and .svelte.ts files. That means shared state is just a module:

cart.svelte.ts
export const cart = $state({ items: [] as { id: string; qty: number }[] });
 
export function add(id: string) {
  const line = cart.items.find((i) => i.id === id);
  if (line) line.qty++;
  else cart.items.push({ id, qty: 1 });
}

In my Svelte 4 dashboard, the order list, the cart, and the loyalty balance each had a writable store with subscribe, update, and a $ prefix in the markup. With runes, they'd be three plain modules. Same language inside and outside components.

Smaller changes you'll notice

  • Props: export let becomes let { title, count = 0 } = $props();
  • Events: on:click becomes a plain onclick attribute. Component events become callback props.
  • Slots: replaced by snippets, {#snippet row(item)} and {@render row(item)}. Snippets can take arguments, which slots never did cleanly.

Migrating

Svelte 5 still runs Svelte 4 components, so you don't need a big-bang rewrite. The CLI can convert a project for you:

npx sv migrate svelte-5

It handles most of the syntax. What it can't decide for you is where $effect should really be $derived. That review is the worth-it part: every one I've looked at so far was a value pretending to be an effect.

Would I pick it again?

Yes. Svelte 4 was easy to start and hard to scale. Runes trade a little of the first for a lot of the second, and the second is where projects spend most of their life.