Skip to content

Commit

Permalink
clarify why we need to use the derived rune
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-vanraes committed Oct 25, 2024
1 parent 18e2284 commit 828502a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion documentation/docs/02-runes/03-$derived.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
title: $derived
---

Derived state is declared with the `$derived` rune:

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.

```svelte
<script>
let count = $state(0);
// this will only set `doubled` on component creation
// it will not update when `count` does
let doubled = count * 2;
</script>
```

We can make any variable based on others reactive by declaring them a Derived state using the `$derived` rune:

```svelte
<script>
Expand Down

0 comments on commit 828502a

Please sign in to comment.