# Svelte 5 Migration Guide ## Overview This guide covers migrating from Svelte 4 to Svelte 5, with focus on the new Runes system which replaces the old reactive syntax. --- ## 🔄 Key Changes at a Glance | Svelte 4 | Svelte 5 | Description | |----------|----------|-------------| | `let count = 0` | `let count = $state(0)` | Reactive state | | `$: doubled = count * 2` | `let doubled = $derived(count * 2)` | Derived values | | `$: { sideEffect() }` | `$effect(() => { sideEffect() })` | Side effects | | `export let prop` | `let { prop } = $props()` | Component props | | `` | `{@render children()}` | Rendering children | | `` | `{@render x?.()}` | Named slots → Snippets | | `$$props`, `$$restProps` | `$props()` with rest | All props access | | `createEventDispatcher()` | Callback props | Event handling | --- ## 📦 State Migration ### Basic State ```svelte ``` ### Object/Array State ```svelte ``` --- ## 🧮 Derived Values Migration ### Simple Derived ```svelte ``` ### Complex Derived ```svelte ``` --- ## ⚡ Effects Migration ### Basic Effects ```svelte ``` ### Pre-Effects (beforeUpdate replacement) ```svelte ``` --- ## 🎁 Props Migration ### Basic Props ```svelte ``` ### Rest Props ```svelte ``` ### Bindable Props (Two-way Binding) ```svelte ``` --- ## 🎰 Slots → Snippets Migration ### Default Slot ```svelte

Card content

{@render children?.()}

Card content

``` ### Named Slots ```svelte

Title

Main content

Footer
{@render header?.()}
{@render children?.()}
{@render footer?.()}
{#snippet header()}

Title

{/snippet}

Main content

{#snippet footer()} Footer {/snippet}
``` ### Slot Props → Snippet Parameters ```svelte {index}: {item.name} {#snippet children({ item, index })} {index}: {item.name} {/snippet} ``` --- ## 📡 Events Migration ### Custom Events ```svelte ``` ### Event Forwarding ```svelte ``` --- ## 🏪 Stores Migration ### Writable Stores ```svelte // stores/counter.svelte.ts export class Counter { count = $state(0); increment() { this.count++; } } export const counter = new Counter(); // Component ``` ### Derived Stores ```svelte

Doubled: {$doubled}

// stores/counter.svelte.ts export class Counter { count = $state(0); doubled = $derived(this.count * 2); } // Component

Doubled: {counter.doubled}

``` --- ## 🔧 Component Lifecycle ### onMount / onDestroy ```svelte ``` --- ## 📋 Migration Checklist ### Phase 1: Preparation - [ ] Update to latest Svelte 4.x - [ ] Fix all deprecation warnings - [ ] Ensure tests are passing - [ ] Review component inventory ### Phase 2: Upgrade - [ ] Update to Svelte 5 - [ ] Update SvelteKit (if using) - [ ] Update dependencies ### Phase 3: Migrate Components - [ ] Convert `export let` → `$props()` - [ ] Convert reactive declarations → `$derived()` - [ ] Convert reactive statements → `$effect()` - [ ] Convert slots → snippets - [ ] Convert events → callback props - [ ] Convert stores → class-based state ### Phase 4: Validation - [ ] Run all tests - [ ] Check for runtime errors - [ ] Performance testing - [ ] Accessibility audit --- ## 🛠️ Automated Migration Svelte 5 includes a migration script: ```bash # Run the migration script npx sv migrate svelte-5 # This will: # - Update syntax to Svelte 5 # - Convert reactive statements # - Update event handlers # - Flag manual review items ``` **Note:** Always review automated changes and test thoroughly!