Skip to content

Commit 87bf5ca

Browse files
committed
docs
1 parent e3ff46e commit 87bf5ca

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

packages/svelte/src/main/ambient.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ declare function $derived<T>(expression: T): T;
6161

6262
declare namespace $derived {
6363
/**
64-
* Sometimes you need to create complex derivations which don't fit inside a short expression.
65-
* In this case, you can resort to `$derived.call` which accepts a function as its argument and returns its value.
64+
* Sometimes you need to create complex derivations that don't fit inside a short expression.
65+
* In these cases, you can use `$derived.call` which accepts a function as its argument.
6666
*
6767
* Example:
6868
* ```ts
69-
* $derived.call(() => {
70-
* let tmp = count;
71-
* if (count > 10) {
72-
* tmp += 100;
73-
* }
74-
* return tmp * 2;
69+
* let total = $derived.call(() => {
70+
* let result = 0;
71+
* for (const n of numbers) {
72+
* result += n;
73+
* }
74+
* return result;
7575
* });
7676
* ```
7777
*
78-
* https://svelte-5-preview.vercel.app/docs/runes#$derived-fn
78+
* https://svelte-5-preview.vercel.app/docs/runes#$derived-call
7979
*/
8080
export function fn<T>(fn: () => T): void;
8181
}

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,29 @@ If the value of a reactive variable is being computed it should be replaced with
134134
```
135135
...`double` will be calculated first despite the source order. In runes mode, `triple` cannot reference `double` before it has been declared.
136136

137-
### `$derived.call`
137+
## `$derived.call`
138138

139-
Sometimes you need to create complex derivations which don't fit inside a short expression. In this case, you can resort to `$derived.call` which accepts a function as its argument and returns its value.
139+
Sometimes you need to create complex derivations that don't fit inside a short expression. In these cases, you can use `$derived.call` which accepts a function as its argument.
140140

141141
```svelte
142142
<script>
143-
let count = $state(0);
144-
let complex = $derived.call(() => {
145-
let tmp = count;
146-
if (count > 10) {
147-
tmp += 100;
143+
let numbers = $state([1, 2, 3]);
144+
let total = $derived.call(() => {
145+
let total = 0;
146+
for (const n of numbers) {
147+
total += n;
148148
}
149-
return tmp * 2;
149+
return total;
150150
});
151151
</script>
152152
153-
<button on:click={() => count++}>
154-
{complex}
153+
<button on:click={() => numbers.push(numbers.length + 1)}>
154+
{numbers.join(' + ')} = {total}
155155
</button>
156156
```
157157

158+
In essence, `$derived(expression)` is equivalent to `$derived.call(() => expression)`.
159+
158160
## `$effect`
159161

160162
To run side-effects like logging or analytics whenever some specific values change, or when a component is mounted to the DOM, we can use the `$effect` rune:

0 commit comments

Comments
 (0)