Skip to content

Commit 828502a

Browse files
clarify why we need to use the derived rune
1 parent 18e2284 commit 828502a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

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

5-
Derived state is declared with the `$derived` rune:
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:
618

719
```svelte
820
<script>

0 commit comments

Comments
 (0)