Skip to content

Commit 0862267

Browse files
Add untracked (#1209)
* Add untracked * Apply suggestions from code review Co-authored-by: Ryan Christian <[email protected]> --------- Co-authored-by: Ryan Christian <[email protected]>
1 parent fe35f68 commit 0862267

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

content/en/guide/v10/signals.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ name.value = "John";
397397

398398
## Reading signals without subscribing to them
399399

400-
On the rare occasion that you need to write to a signal inside [`effect(fn)`](#effectfn), but don't want the effect to re-run when that signal changes, you can use `.peek()` to get the signal's current value without subscribing.
400+
On the rare occasion that you need to write to a signal inside [`effect(fn)`](#effectfn), but don't want the effect to re-run when that signal changes,
401+
you can use `.peek()` to get the signal's current value without subscribing.
401402

402403
```js
403404
const delta = signal(0);
@@ -417,6 +418,22 @@ count.value = 10;
417418

418419
> :bulb: Tip: The scenarios in which you don't want to subscribe to a signal are rare. In most cases you want your effect to subscribe to all signals. Only use `.peek()` when you really need to.
419420
421+
As an alternative to `.peek()`, we have the `untracked` function which receives a function as an argument and returns the outcome of the function. In `untracked` you can
422+
reference any signal with `.value` without creating a subscription. This can come in handy when you have a reusable function that accesses `.value` or you need to access
423+
more than 1 signal.
424+
425+
```js
426+
const delta = signal(0);
427+
const count = signal(0);
428+
429+
effect(() => {
430+
// Update `count` without subscribing to `count` or `delta`:
431+
count.value = untracked(() => {
432+
count.value + delta.value
433+
});
434+
});
435+
```
436+
420437

421438
## Combining multiple updates into one
422439

@@ -551,3 +568,18 @@ batch(() => {
551568
surname.value = "Smith";
552569
});
553570
```
571+
572+
### untracked(fn)
573+
574+
The `untracked(fn)` function can be used to access the value of several signals without subscribing to them.
575+
576+
```js
577+
const name = signal("Jane");
578+
const surname = signal("Doe");
579+
580+
effect(() => {
581+
untracked(() => {
582+
console.log(`${name.value} ${surname.value}`)
583+
})
584+
})
585+
```

0 commit comments

Comments
 (0)