Skip to content

Commit

Permalink
Add untracked (#1209)
Browse files Browse the repository at this point in the history
* Add untracked

* Apply suggestions from code review

Co-authored-by: Ryan Christian <[email protected]>

---------

Co-authored-by: Ryan Christian <[email protected]>
  • Loading branch information
JoviDeCroock and rschristian authored Dec 17, 2024
1 parent fe35f68 commit 0862267
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion content/en/guide/v10/signals.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ name.value = "John";

## Reading signals without subscribing to them

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.
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.

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

> :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.
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
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
more than 1 signal.

```js
const delta = signal(0);
const count = signal(0);

effect(() => {
// Update `count` without subscribing to `count` or `delta`:
count.value = untracked(() => {
count.value + delta.value
});
});
```


## Combining multiple updates into one

Expand Down Expand Up @@ -551,3 +568,18 @@ batch(() => {
surname.value = "Smith";
});
```

### untracked(fn)

The `untracked(fn)` function can be used to access the value of several signals without subscribing to them.

```js
const name = signal("Jane");
const surname = signal("Doe");

effect(() => {
untracked(() => {
console.log(`${name.value} ${surname.value}`)
})
})
```

0 comments on commit 0862267

Please sign in to comment.