Svelte 5 Runes: Complete Migration Guide from Svelte 4 Stores
Svelte 5 Runes: Complete Migration Guide from Svelte 4 Stores
<script>
export let count = 0;
$: doubled = count * 2;
$: console.log('Count changed:', count);
</script><script>
let count = $state(0);
let doubled = $derived(count * 2);
$effect(() => {
console.log('Count changed:', count);
});
</script>// store.js
import { writable } from 'svelte/store';
export const theme = writable('dark');// theme.svelte.js
let theme = $state('dark');
export function getTheme() { return theme; }
export function setTheme(val) { theme = val; }Svelte 5 introduced Runes, a fundamentally new reactivity system that replaces the old store-based and let-based reactivity from Svelte 4. If you are planning to migrate an existing Svelte 4 project, this guide covers everything you need to know.
Why Runes?
In Svelte 4, reactivity was implicit and tied to the compiler. Variables declared with let were reactive in components, and stores used a $ prefix for auto-subscription. This worked well but had limitations with code reuse and understanding reactivity boundaries. Runes make reactivity explicit and portable across files.
Key Runes You Need to Know
1. $state - Declares reactive state. Replaces plain let declarations.
2. $derived - Creates derived values that auto-update. Replaces $: reactive declarations.
3. $effect - Runs side effects when dependencies change. Replaces $: blocks with side effects.
4. $props - Declares component props. Replaces export let.
5. $bindable - Makes a prop bindable from the parent.
Migration Examples
Svelte 4:
<script>
export let count = 0;
$: doubled = count * 2;
$: console.log('Count changed:', count);
</script><script>
let count = $state(0);
let doubled = $derived(count * 2);
$effect(() => {
console.log('Count changed:', count);
});
</script>// store.js
import { writable } from 'svelte/store';
export const theme = writable('dark');// theme.svelte.js
let theme = $state('dark');
export function getTheme() { return theme; }
export function setTheme(val) { theme = val; }