Sub topic 8
Reading signals without tracking them
This chapter assumes you can write signal(), computed() and effect(), and that you know a computed signal records the signals it reads as dependencies. If "dependency" or "dynamic tracking" is unfamiliar, review the earlier chapters on those topics first.
Why you sometimes want a read that does not count
Angular records dependencies automatically. When a computed() or effect() callback runs, every signal read during that run becomes a dependency. If any of them changes, the computed value is invalidated or the effect is scheduled to run again.
Usually this is what you want. Sometimes you need a signal's current value without being re-run when that signal changes. Typical cases:
- An effect should fire when
querychanges, but it also wants to include the currentpagein a log line or request. - An effect calls a helper function, and you don't know or don't want to know which signals the helper reads internally.
- A computed value should recalculate for one input, but treat another as a snapshot.
Angular provides untracked() for this. It runs a function and reads inside it do not create dependencies for the surrounding reactive context.
The basic API
untracked is imported from @angular/core. It takes a function and returns that function's result.
import { Component, signal, effect, untracked } from '@angular/core';
@Component({
selector: 'app-search',
template: `
<input (input)="query.set($any($event.target).value)" />
<button (click)="page.update((p) => p + 1)">Next page ({{ page() }})</button>
`,
})
export class SearchComponent {
query = signal('');
page = signal(1);
constructor() {
effect(() => {
const q = this.query();
const p = untracked(() => this.page());
console.log('search', q, 'page', p);
});
}
}The effect reads query() directly, so query is a dependency. It reads page() inside untracked, so page is not. Typing in the input re-runs the effect and logs the latest page. Clicking the button changes page but does not log anything.
Because a signal is itself a function, you can pass it directly when you only need a single read: untracked(this.page). Use the arrow form when you need more than one read or some logic around it.
effect(() => {
const q = this.query();
const snapshot = untracked(() => ({
page: this.page(),
pageSize: this.pageSize(),
}));
this.load(q, snapshot.page, snapshot.pageSize);
});Grouping the untracked reads in one block makes it obvious which signals are triggers and which are context.
What the tracking looks like
The diagram shows one run of the search effect. Only the read of query crosses into the dependency set.
Hidden reads inside function calls
The most common reason to reach for untracked() is not a read you wrote yourself. It is a read inside code you call.
Tracking is not limited to the lines of your callback. It applies to everything that runs during the callback, including methods on services, helper functions and library code. If any of them read a signal, your effect depends on it.
import { Injectable, Component, signal, effect, untracked } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class Logger {
level = signal<'info' | 'debug'>('info');
log(message: string) {
// Reads this.level(), which is a signal read.
console.log(`[${this.level()}] ${message}`);
}
}
@Component({
selector: 'app-tracker',
template: ``,
})
export class TrackerComponent {
userId = signal(1);
constructor(private logger: Logger) {
effect(() => {
const id = this.userId();
// Without untracked, a change to logger.level would re-run this effect.
untracked(() => this.logger.log(`user changed to ${id}`));
});
}
}Without the untracked wrapper, flipping logger.level from 'info' to 'debug' would re-run the effect and write a misleading "user changed" line, even though the user did not change. The component author never wrote level(), so nothing in the effect body hints at the dependency.
A useful habit: in an effect, read the signals that should trigger it at the top level. Wrap calls to anything else (services, stores, callbacks) in untracked(), unless you deliberately want them as triggers.
Using untracked inside computed()
untracked() also works inside computed(), but it deserves more caution there. A computed signal represents a pure derivation, and a value that ignores some of its inputs can quietly become stale.
import { signal, computed, untracked } from '@angular/core';
const a = signal(1);
const b = signal(10);
const sum = computed(() => a() + untracked(b));
console.log(sum()); // 11
b.set(20);
console.log(sum()); // 11, cached: b is not a dependency
a.set(2);
console.log(sum()); // 22, recalculated because a changed and b is read freshThe result after b.set(20) is the surprise. The displayed value no longer matches the current inputs, and it only catches up when a changes for some other reason. The computed value now depends on when it happened to be recalculated.
If you find yourself writing this, ask whether the logic should be a computed at all. Two better options:
- Make both signals real dependencies, so the computed always reflects its inputs.
- If you need "the value of
bat the momentachanged", considerlinkedSignal, covered in a later chapter, or an event handler that captures the value explicitly.
Untracked reads versus untracked writes
untracked() affects reads. It does nothing special for writes, and a write does not create a dependency.
A pattern you will see: an effect reads a signal as a trigger, then writes to a different signal. The write does not make the effect depend on the target. The danger is different: if the effect reads the same signal it writes, it can trigger itself.
import { Component, signal, effect, untracked } from '@angular/core';
@Component({
selector: 'app-visit-counter',
template: `{{ visits() }}`,
})
export class VisitCounterComponent {
route = signal('/home');
visits = signal(0);
constructor() {
effect(() => {
this.route(); // trigger
// update() reads the current value of visits internally.
// Writing with update is not tracked as a dependency, so this does not loop.
this.visits.update((n) => n + 1);
});
}
}update() reads the current value for you, and that internal read is not tracked, so the effect does not re-trigger itself. If you wrote this.visits.set(this.visits() + 1) instead, visits() would be a tracked read and the effect would depend on the signal it writes. Prefer update() for read-modify-write inside effects, or wrap the read in untracked().
Also ask whether the effect should exist. Setting one signal from another is often a sign that a computed() or linkedSignal() fits better, as the earlier chapter on choosing between computed, effect and event handlers explained.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Calling a service method in an effect without `untracked` | Effect re-runs when unrelated service state changes | Wrap the call in `untracked()` |
| Reading everything at the top of the effect "to be safe" | Effect fires more often than intended | Read only triggers at top level; put context reads in `untracked()` |
| Wrapping the trigger signal in `untracked()` | Effect runs once and never again | Read the trigger outside `untracked()` |
| Using `untracked()` in `computed()` to silence a dependency | Stale computed value | Make the dependency real, or move the logic out of `computed()` |
| `set(x() + 1)` inside an effect on the same signal | Effect re-triggers itself | Use `update()` or `untracked()` |
The third row is easy to do accidentally. If an effect reads all its signals inside untracked, it has no dependencies. It runs once when created and then never again. Angular does not warn you about this.
effect(() => {
// Bug: no tracked reads at all. This effect runs once.
untracked(() => console.log(this.query(), this.page()));
});Asynchronous code is untracked by default
Tracking only covers code that runs synchronously during the callback. Reads that happen after an await, inside a setTimeout callback or inside a promise then handler are not tracked, because by then the reactive context has finished.
effect(() => {
const id = this.id(); // tracked
setTimeout(() => {
console.log(this.other()); // not tracked: runs after the effect body returns
}, 100);
});This can be useful, but it is also a source of confusion: an effect that reads a signal only after an await does not depend on it. Read the signals you need as local constants before the first await, and then use the constants.
Choosing between tracked and untracked
Use this checklist when writing an effect:
- List what should cause the effect to run. Read exactly those signals at the top level.
- List what the effect merely needs to know when it runs. Read those inside
untracked(). - For every function you call, decide whether you want its internal signal reads as triggers. If not, wrap the call.
- If you are tempted to use
untracked()insidecomputed(), check whether the logic really belongs in a computed.
Exercise
This effect should send an analytics event whenever screen changes. It currently sends an extra event whenever user changes too, because analytics.track reads user() internally to attach the user name. Rewrite the effect so only screen triggers it, and say what happens if you also wrap the this.screen() read in untracked().
effect(() => {
this.analytics.track('view', this.screen());
});
Show answer
Read the trigger at the top level and wrap the service call:
effect(() => {
const s = this.screen();
untracked(() => this.analytics.track('view', s));
});
Now screen is the only dependency, because user() is read inside untracked. If you also wrapped this.screen() in untracked(), the effect would have no dependencies. It would run once when created and never again when screen changes.
Exercise
Given const a = signal(1); const b = signal(10); const sum = computed(() => a() + untracked(b));, what do three consecutive calls to sum() return in this sequence: read, then b.set(50), then read, then a.set(2), then read?
Show answer
The first read returns 11. After b.set(50) the second read still returns 11, because b is not a dependency and the cached value is reused. After a.set(2) the computed is invalidated, so the third read recalculates as 2 + 50 and returns 52.