Index of every page on this site: llms.txt
All skills
spences10 avatar

/svelte-runes

@5fe4989
Run it yourself

Svelte runes guidance. Use for reactive state, props, effects, or migration. Covers $state, $derived, $effect, $props, $bindable. Prevents reactivity mistakes.

Use this Skill: https://skilld.dev/gh/spences10/svelte-skills-kit/svelte-runes

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesmigration-gotchas.md

≈1.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Migration Gotchas: Svelte 4 → Svelte 5

Quick Translation Table

Svelte 4 Svelte 5 Notes
let count = 0 let count = $state(0) Make reactive
$: doubled = count * 2 let doubled = $derived(count * 2) Computed value
$: { console.log(count); } $effect(() => { console.log(count); }) Side effect
$: if (count > 10) { ... } $effect(() => { if (count > 10) { ... } }) Conditional effect
export let name let { name } = $props() Props
export let value (bindable) let { value = $bindable() } = $props() Two-way binding
on:click={handler} onclick={handler} Event handler
on:click|preventDefault onclick={(e) => { e.preventDefault(); ... }} Event modifier
<slot /> {@render children()} Default slot
<slot name="header" /> {@render header()} Named slot
N/A {#snippet name()}...{/snippet} Define reusable markup

Critical Differences

1. Reactive Statements → Runes

Svelte 4:

<script>
	let count = 0;
	$: doubled = count * 2; // Computed
	$: {
		// Effect
		console.log(count);
		document.title = `Count: ${count}`;
	}
</script>

Svelte 5:

<script>
	let count = $state(0);
	let doubled = $derived(count * 2); // Computed

	$effect(() => {
		// Effect
		console.log(count);
		document.title = `Count: ${count}`;
	});
</script>

Why: Runes are more explicit about intent (derived vs effect) and avoid ambiguity.

2. Props

Svelte 4:

<script>
	export let name;
	export let age = 18;
</script>

Svelte 5:

<script>
	let { name, age = 18 } = $props();
</script>

With rest props:

<script>
	let { name, age, ...rest } = $props();
</script>

<div {...rest}>{name}</div>

3. Two-Way Binding (bind:)

Svelte 4:

<!-- Child.svelte -->
<script>
	export let value;
</script>

<input bind:value />

<!-- Parent: <Child bind:value={text} /> -->

Svelte 5:

<!-- Child.svelte -->
<script>
	let { value = $bindable() } = $props();
</script>

<input bind:value />

<!-- Parent: <Child bind:value={text} /> -->

Why: Explicit $bindable() makes it clear which props support two-way binding.

4. Event Handlers

Svelte 4:

<button on:click={handleClick}>Click</button>
<button on:click|preventDefault={handleClick}>Click</button>
<button on:click={() => count++}>Increment</button>

Svelte 5:

<button onclick={handleClick}>Click</button>
<button
	onclick={(e) => {
		e.preventDefault();
		handleClick(e);
	}}>Click</button
>
<button onclick={() => count++}>Increment</button>

Why: Standard DOM properties instead of directives.

5. Slots → Children & Snippets

Svelte 4:

<!-- Layout.svelte -->
<div class="layout">
	<header><slot name="header" /></header>
	<main><slot /></main>
</div>

<!-- Usage -->
<Layout>
	<div slot="header">Header content</div>
	Main content
</Layout>

Svelte 5:

<!-- Layout.svelte -->
<script>
	let { header, children } = $props();
</script>

<div class="layout">
	<header>{@render header()}</header>
	<main>{@render children()}</main>
</div>

<!-- Usage -->
<Layout>
	{#snippet header()}
		Header content
	{/snippet}
	Main content
</Layout>

Why: More explicit and composable.

6. Lifecycle Functions

Svelte 4:

<script>
	import { onMount, onDestroy } from 'svelte';

	onMount(() => {
		console.log('mounted');
		return () => console.log('cleanup');
	});

	onDestroy(() => {
		console.log('destroyed');
	});
</script>

Svelte 5:

<script>
  import { onMount } from 'svelte';

  onMount(() => {
    console.log('mounted');
    return () => console.log('cleanup');
  });

  // For most cleanup, use $effect:
  $effect(() => {
    const interval = setInterval(() => {...}, 1000);
    return () => clearInterval(interval);
  });
</script>

Why: $effect with cleanup function covers most onDestroy use cases.

Common Migration Mistakes

❌ Mixing Svelte 4 and 5 Syntax

<!-- WRONG - DON'T MIX -->
<script>
	let count = $state(0);
	$: doubled = count * 2; // Mixing runes with reactive statements!
</script>

Fix: Use runes consistently:

<script>
	let count = $state(0);
	let doubled = $derived(count * 2);
</script>

❌ Forgetting to Use $state

<!-- WRONG -->
<script>
	let count = 0; // Not reactive in Svelte 5!
</script>

<button onclick={() => count++}>{count}</button>
<!-- UI won't update! -->

Fix:

<script>
	let count = $state(0);
</script>

<button onclick={() => count++}>{count}</button>

❌ Using on: Instead of onclick

<!-- WRONG -->
<button on:click={handler}>Click</button>

<!-- RIGHT -->
<button onclick={handler}>Click</button>

❌ Forgetting {@render} for Children

<!-- WRONG -->
<script>
	let { children } = $props();
</script>

<div>{children}</div>
<!-- Won't render! -->

<!-- RIGHT -->
<div>{@render children()}</div>

❌ Trying to Bind Without $bindable

<!-- Child.svelte - WRONG -->
<script>
  let { value } = $props();  // Not bindable!
</script>

<!-- Parent tries: <Child bind:value={text} /> -->
<!-- Will error! -->

<!-- Child.svelte - RIGHT -->
<script>
  let { value = $bindable() } = $props();
</script>

Stores Still Work!

Svelte stores (writable, readable, derived) still work in Svelte 5:

<script>
	import { writable } from 'svelte/store';

	const count = writable(0);
</script>

<button onclick={() => $count++}>{$count}</button>

When to use stores vs runes:

  • Runes: Component-local state
  • Stores: Global state, shared across components

TypeScript Changes

Svelte 4:

<script lang="ts">
	export let count: number;
</script>

Svelte 5:

<script lang="ts">
	interface Props {
		count: number;
	}

	let { count }: Props = $props();
</script>

Reactive Class Fields

Svelte 5 introduces reactive class fields:

<script>
	class Counter {
		count = $state(0);
		doubled = $derived(this.count * 2);

		increment() {
			this.count++;
		}
	}

	const counter = new Counter();
</script>

<button onclick={() => counter.increment()}>
	{counter.count} (doubled: {counter.doubled})
</button>

Migration Strategy

  1. Don't mix syntaxes - Migrate one component at a time fully
  2. Start with leaf components - Migrate from bottom up
  3. Test incrementally - Ensure each component works before moving on
  4. Use TypeScript - Catch binding/prop errors at compile time
  5. Read the migration guide - https://svelte.dev/docs/svelte/v5-migration-guide

Feature Detection

If you need to support both Svelte 4 and 5:

<script>
	import { VERSION } from 'svelte/compiler';

	const isSvelte5 = VERSION.startsWith('5');

	// Conditionally use syntax based on version
</script>

But: Generally better to fully migrate to Svelte 5 than maintain dual syntax.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The analyzed Svelte 5 documentation skill contains detailed guidance, architectural definitions, and best practices for implementing Svelte 5 runes. There are no executable scripts, remote package installations, prompt injections, or obfuscated assets detected in the files.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer8mo

    9 files scanned · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 5fe4989. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago

README badge

README badge for spences10/svelte-skills-kit/svelte-runes