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/02-runes/03-$derived.md
+3-13Lines changed: 3 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,7 @@
2
2
title: $derived
3
3
---
4
4
5
-
6
-
Svelte's `<script>` blocks are run only when the component is created, so assignments within a `<script>` block are not automatically run again when a prop updates.
7
-
8
-
```svelte
9
-
<script>
10
-
let count = $state(0);
11
-
// this will only set `doubled` on component creation
12
-
// it will not update when `count` does
13
-
let doubled = count * 2;
14
-
</script>
15
-
```
16
-
17
-
We can make any variable based on others reactive by declaring them a Derived state using the `$derived` rune:
5
+
Derived state is declared with the `$derived` rune:
18
6
19
7
```svelte
20
8
<script>
@@ -33,6 +21,8 @@ The expression inside `$derived(...)` should be free of side-effects. Svelte wil
33
21
34
22
As with `$state`, you can mark class fields as `$derived`.
35
23
24
+
> [!NOTE] Code in Svelte components is only executed once at creation, without the `$derived` rune `double` would maintain it's original value.
25
+
36
26
## `$derived.by`
37
27
38
28
Sometimes you need to create complex derivations that don't fit inside a short expression. In these cases, you can use `$derived.by` which accepts a function as its argument.
0 commit comments