Skip to content

Commit be931e3

Browse files
put clarification as note
1 parent 828502a commit be931e3

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

documentation/docs/02-runes/03-$derived.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@
22
title: $derived
33
---
44

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:
186

197
```svelte
208
<script>
@@ -33,6 +21,8 @@ The expression inside `$derived(...)` should be free of side-effects. Svelte wil
3321

3422
As with `$state`, you can mark class fields as `$derived`.
3523

24+
> [!NOTE] Code in Svelte components is only executed once at creation, without the `$derived` rune `double` would maintain it's original value.
25+
3626
## `$derived.by`
3727

3828
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

Comments
 (0)