Sub topic 10
Fine-Tuning Reactivity: Equality and untracked()
This chapter assumes you already know how to create writable signals, derive values with computed(), and run side effects with effect(). It covers two tools that control when those consumers react. Equality functions decide whether a new value counts as a change. untracked() lets you read a signal without depending on it.
How Signals Decide Something Changed
When you call set() or update(), the signal compares the new value with the old one. By default it uses Object.is. If the two values are equal, nothing happens: the signal does not notify its consumers, and no computed signal, effect, or template recomputes.
Comparing with Object.is works well for primitives. For objects and arrays it compares references, which leads to the most common mistake in this area:
import { signal } from '@angular/core';
const tags = signal<string[]>(['a']);
// Wrong: same array reference, Object.is returns true, nobody is notified
tags.update(list => { list.push('b'); return list; });
// Right: new array reference, consumers are notified
tags.update(list => [...list, 'b']);Treat signal values as immutable. Replace objects and arrays instead of mutating them.
Custom Equality Functions
Sometimes a reference check is too strict. If your app fetches a fresh User object with the same data, every consumer reruns even though nothing meaningful changed. You can pass an equal option to signal() or computed() to define what "the same" means.
import { Component, effect, signal, untracked } from '@angular/core';
interface User { id: number; name: string; }
@Component({ selector: 'app-user-log', template: '' })
export class UserLogComponent {
user = signal<User>(
{ id: 1, name: 'Ada' },
{ equal: (a, b) => a.id === b.id }
);
visits = signal(0);
constructor() {
effect(() => {
const current = this.user();
const count = untracked(() => this.visits());
console.log(`User ${current.name}, visits so far: ${count}`);
});
}
}Here, setting a user with the same id counts as no change. This has a cost. If you call this.user.set({ id: 1, name: 'Ada Lovelace' }), the signal keeps the old value and the effect never sees the new name. An equality function should only treat two values as equal when no consumer could care about the difference between them.
On computed(), an equality function stops changes from spreading further. The computed still recomputes when its inputs change, but if the result is "equal" to the previous one, its own dependents do not rerun:
import { computed, signal } from '@angular/core';
const numbers = signal([1, 2, 3, 4]);
const evens = computed(
() => numbers().filter(n => n % 2 === 0),
{ equal: (a, b) => a.length === b.length && a.every((v, i) => v === b[i]) }
);
numbers.set([1, 2, 3, 4, 5]); // evens recomputes to [2, 4]; dependents are not notifiedReading Without Tracking: untracked()
Every signal read inside a computed() or effect() becomes a dependency. In the component above, the effect should log when the user changes but not every time visits ticks. Wrapping the read in untracked() returns the current value without registering a dependency.
untracked() also protects you from hidden dependencies. If an effect calls a service method that reads signals internally, those reads are tracked too. Wrap the call so the effect depends only on what you intended:
effect(() => {
const id = this.user().id;
untracked(() => this.analytics.track('user-changed', id));
});Common Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| UI does not update after `update()` | Object or array mutated in place | Return a new reference |
| Changed field never appears | Custom `equal` ignores that field | Compare every field consumers use |
| Effect reruns too often | Signal reads hidden inside a called function | Wrap the call in `untracked()` |
| Effect never reruns for a value | That value was read inside `untracked()` | Read it directly if the effect should react |
Exercise
A signal settings = signal({ theme: 'light' }) is updated with settings.update(s => { s.theme = 'dark'; return s; }). A template showing settings().theme still shows "light". Why, and how do you fix it?
Show answer
The update function returns the same object reference, so the default Object.is check reports no change and consumers are not notified. Return a new object instead: settings.update(s => ({ ...s, theme: 'dark' })).
Exercise
An effect should save form() to the server whenever it changes, and include the current userId() in the request without rerunning when only userId changes. How do you write the reads?
Show answer
Read form() normally so it is tracked, and read the id with untracked(() => this.userId()) so it is not a dependency.