You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/07-misc/07-v5-migration-guide.md
+20-19Lines changed: 20 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,13 @@ You don't have to migrate to the new syntax right away - Svelte 5 still supports
10
10
11
11
At the heart of Svelte 5 is the new runes API. Runes are basically compiler instructions that inform Svelte about reactivity. Syntactically, runes are functions starting with a dollar-sign.
12
12
13
-
### let -> $state
13
+
### let → $state
14
14
15
15
In Svelte 4, a `let` declaration at the top level of a component was implicitly reactive. In Svelte 5, things are more explicit: a variable is reactive when created using the `$state` rune. Let's migrate the counter to runes mode by wrapping the counter in `$state`:
16
16
17
17
```svelte
18
18
<script>
19
-
let count = +++$state(+++0+++)+++;
19
+
let count = +++$state(0)+++;
20
20
</script>
21
21
```
22
22
@@ -25,14 +25,14 @@ Nothing else changes. `count` is still the number itself, and you read and write
25
25
> [!DETAILS] Why we did this
26
26
> `let` being implicitly reactive at the top level worked great, but it meant that reactivity was constrained - a `let` declaration anywhere else was not reactive. This forced you to resort to using stores when refactoring code out of the top level of components for reuse. This meant you had to learn an entirely separate reactivity model, and the result often wasn't as nice to work with. Because reactivity is more explicit in Svelte 5, you can keep using the same API outside the top level of components. Head to [the tutorial](/tutorial) to learn more.
27
27
28
-
### $: -> $derived/$effect
28
+
### $: → $derived/$effect
29
29
30
30
In Svelte 4, a `$:` statement at the top level of a component could be used to declare a derivation, i.e. state that is entirely defined through a computation of other state. In Svelte 5, this is achieved using the `$derived` rune:
@@ -42,7 +42,8 @@ A `$:` statement could also be used to create side effects. In Svelte 5, this is
42
42
43
43
```svelte
44
44
<script>
45
-
let count = +++$state(+++0+++)+++;
45
+
let count = $state(0);
46
+
46
47
---$:---+++$effect(() =>+++ {
47
48
if (count > 5) {
48
49
alert('Count is too high!');
@@ -73,14 +74,14 @@ Note that [when `$effect` runs is different]($effect#Understanding-dependencies)
73
74
> - executing dependencies as needed and therefore being immune to ordering problems
74
75
> - being TypeScript-friendly
75
76
76
-
### export let -> $props
77
+
### export let → $props
77
78
78
79
In Svelte 4, properties of a component were declared using `export let`. Each property was one declaration. In Svelte 5, all properties are declared through the `$props` rune, through destructuring:
@@ -192,9 +193,9 @@ This function is deprecated in Svelte 5. Instead, components should accept _call
192
193
```svelte
193
194
<!--- file: Pump.svelte --->
194
195
<script>
195
-
---import { createEventDispatcher } from 'svelte';
196
-
const dispatch = createEventDispatcher();
197
-
---
196
+
---import { createEventDispatcher } from 'svelte';---
197
+
---const dispatch = createEventDispatcher();---
198
+
198
199
+++let { inflate, deflate } = $props();+++
199
200
let power = $state(5);
200
201
</script>
@@ -466,11 +467,11 @@ By now you should have a pretty good understanding of the before/after and how t
466
467
We thought the same, which is why we provide a migration script to do most of the migration automatically. You can upgrade your project by using `npx sv migrate svelte-5`. This will do the following things:
467
468
468
469
- bump core dependencies in your `package.json`
469
-
- migrate to runes (`let`->`$state` etc)
470
-
- migrate to event attributes for DOM elements (`on:click`->`onclick`)
471
-
- migrate slot creations to render tags (`<slot />`->`{@render children()}`)
472
-
- migrate slot usages to snippets (`<div slot="x">...</div>`->`{#snippet x()}<div>...</div>{/snippet}`)
You can also migrate a single component in VS Code through the `Migrate Component to Svelte 5 Syntax` command, or in our Playground through the `Migrate` button.
- fix: add `files` and `group` to HTMLInputAttributes in elements.d.ts ([#15492](https://github.com/sveltejs/svelte/pull/15492))
18
+
19
+
- fix: throw rune_invalid_arguments_length when $state.raw() is used with more than 1 arg ([#15516](https://github.com/sveltejs/svelte/pull/15516))
0 commit comments