Sub topic 8
Reading signals without tracking them
This chapter assumes you know how to create writable signals, derive values with computed(), and run an effect() inside an injection context. Earlier chapters showed that Angular records every signal read during an evaluation as a dependency. Here you will learn how to read a signal on purpose without creating that dependency.
The problem: too many dependencies
An effect re-runs when any signal it reads changes. Sometimes you need a signal's current value, but you do not want a change to that signal to trigger the effect.
Take a search component. The effect should run when the query changes. It should not run again when the user only moves to another page. If the effect reads both query() and page(), it depends on both.
import { Component, signal, effect } from '@angular/core';
@Component({
selector: 'app-search-naive',
template: ``,
})
export class SearchNaiveComponent {
query = signal('');
page = signal(1);
constructor() {
effect(() => {
// Reads both signals, so both are dependencies.
console.log('search', this.query(), 'page', this.page());
});
}
}Changing page runs this effect again, which is not what we wanted.
Using untracked()
untracked() takes a function, runs it, and returns its result. Any signal read inside that function is not recorded as a dependency of the surrounding effect or computed.
import { Component, signal, effect, untracked } from '@angular/core';
@Component({
selector: 'app-search',
template: ``,
})
export class SearchComponent {
query = signal('');
page = signal(1);
constructor() {
effect(() => {
const q = this.query();
untracked(() => console.log('search', q, 'page', this.page()));
});
}
}The effect reads query() outside untracked(), so query is a dependency. It reads page() inside, so page is not. The code inside the callback still sees the current value of page. It simply does not subscribe to it.
Where untracked() is most useful
Use it when an effect needs a value as context but should not react to it. Common cases:
- Reading a "current user" or "current page" signal to include in a log or request.
- Calling a method or service that reads signals internally. Those reads would otherwise become dependencies of your effect, and you may not control them.
- Writing to a signal from inside an effect, when the write logic also reads other signals.
The second case is easy to miss. A call such as this.service.save(data) may read several signals in its implementation. Wrapping the call in untracked() keeps your effect's dependencies limited to what you read directly.
import { Component, signal, effect, untracked, inject } from '@angular/core';
class AnalyticsService {
sessionId = signal('abc');
track(event: string) {
console.log('track', event, this.sessionId());
}
}
@Component({
selector: 'app-page-view',
template: ``,
})
export class PageViewComponent {
private analytics = inject(AnalyticsService);
route = signal('/home');
constructor() {
effect(() => {
const route = this.route();
// track() reads sessionId; without untracked() it would become a dependency.
untracked(() => this.analytics.track(route));
});
}
}For this example to run, provide AnalyticsService in the root injector, for instance with @Injectable({ providedIn: 'root' }).
Using untracked() in computed()
untracked() also works inside computed(). This is rarer, because a computed value is normally supposed to follow everything it reads. If you exclude a read, the computed value can become stale: it will not update when the excluded signal changes, and it will show the old result until another dependency changes.
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, still cached because b is not a dependency
a.set(2);
console.log(sum()); // 22, recomputed because a changedCommon mistakes
- Wrapping the signal you do want to react to. If
query()is insideuntracked(), the effect never re-runs when the query changes. - Reading a signal outside
untracked()by accident, for example through a helper method. Check what a called function reads. - Using
untracked()to hide a design problem. If you keep excluding many reads, consider whether the logic belongs in an event handler instead.
Exercise
An effect reads userId() and then calls logger.log('loaded'). The logger reads a verbose signal internally. The effect re-runs whenever verbose changes, and you want it to run only when userId changes. What do you change?
Show answer
Wrap the logger call in untracked(): untracked(() => logger.log('loaded')). The read of verbose inside the logger then no longer counts as a dependency of the effect, while userId() stays tracked because it is read outside untracked().
Exercise
Given const total = computed(() => a() + untracked(() => b())), what does total() return after b.set(5) if nothing else has changed since the last read?
Show answer
It returns the previously cached value. b is not a dependency of total, so setting it does not invalidate the cache. The new value of b appears only after a changes and total is recomputed.