Skip to content

Commit 6a2c28c

Browse files
authored
docs: tweak untrack description, provide an example of usage (#14085)
* docs: tweak untrack description, provide an example of usage * link to untrack * add derived docs too
1 parent 2ab7004 commit 6a2c28c

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ Sometimes you need to create complex derivations that don't fit inside a short e
4545
```
4646

4747
In essence, `$derived(expression)` is equivalent to `$derived.by(() => expression)`.
48+
49+
## Understanding dependencies
50+
51+
Anything read synchronously inside the `$derived` expression (or `$derived.by` function body) is considered a _dependency_ of the derived state. When the state changes, the derived will be marked as _dirty_ and recalculated when it is next read.
52+
53+
To exempt a piece of state from being treated as a dependency, use [`untrack`](svelte#untrack).

documentation/docs/02-runes/04-$effect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: $effect
33
---
44

5-
Effects are what make your application _do things_. When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed, and re-runs the function when that state later changes.
5+
Effects are what make your application _do things_. When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed (unless accessed inside [`untrack`](svelte#untrack)), and re-runs the function when that state later changes.
66

77
Most of the effects in a Svelte app are created by Svelte itself — they're the bits that update the text in `<h1>hello {name}!</h1>` when `name` changes, for example.
88

packages/svelte/src/internal/client/runtime.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,17 @@ export function invalidate_inner_signals(fn) {
831831
}
832832

833833
/**
834-
* Use `untrack` to prevent something from being treated as an `$effect`/`$derived` dependency.
834+
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
835+
* any state read inside `fn` will not be treated as a dependency.
836+
*
837+
* ```ts
838+
* $effect(() => {
839+
* // this will run when `data` changes, but not when `time` changes
840+
* save(data, {
841+
* timestamp: untrack(() => time)
842+
* });
843+
* });
844+
* ```
835845
* @template T
836846
* @param {() => T} fn
837847
* @returns {T}

packages/svelte/types/index.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,17 @@ declare module 'svelte' {
455455
* */
456456
export function tick(): Promise<void>;
457457
/**
458-
* Use `untrack` to prevent something from being treated as an `$effect`/`$derived` dependency.
458+
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
459+
* any state read inside `fn` will not be treated as a dependency.
460+
*
461+
* ```ts
462+
* $effect(() => {
463+
* // this will run when `data` changes, but not when `time` changes
464+
* save(data, {
465+
* timestamp: untrack(() => time)
466+
* });
467+
* });
468+
* ```
459469
* */
460470
export function untrack<T>(fn: () => T): T;
461471
/**

0 commit comments

Comments
 (0)