You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add untracked
* Apply suggestions from code review
Co-authored-by: Ryan Christian <[email protected]>
---------
Co-authored-by: Ryan Christian <[email protected]>
Copy file name to clipboardExpand all lines: content/en/guide/v10/signals.md
+33-1Lines changed: 33 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -397,7 +397,8 @@ name.value = "John";
397
397
398
398
## Reading signals without subscribing to them
399
399
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.
401
402
402
403
```js
403
404
constdelta=signal(0);
@@ -417,6 +418,22 @@ count.value = 10;
417
418
418
419
> :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.
419
420
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
+
constdelta=signal(0);
427
+
constcount=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
+
420
437
421
438
## Combining multiple updates into one
422
439
@@ -551,3 +568,18 @@ batch(() => {
551
568
surname.value="Smith";
552
569
});
553
570
```
571
+
572
+
### untracked(fn)
573
+
574
+
The `untracked(fn)` function can be used to access the value of several signals without subscribing to them.
0 commit comments