Sub topic 11
Resettable State with linkedSignal()
This chapter assumes you already know how to create writable signals with signal(), set(), and update(), and how computed() derives read-only values. You should also be comfortable writing standalone Angular components.
The gap between signal() and computed()
Some state sits between the two primitives you already know. A computed() signal stays in sync with its source, but you cannot write to it. A signal() accepts writes, but it ignores changes to anything else.
Take a size picker. The selected size should default to the first available option. The user should be able to pick a different one. When the list of options changes, the selection should reset. Neither primitive covers all three behaviors without an effect that copies values around, and copying signals with effects is an anti-pattern.
linkedSignal() creates a writable signal whose value is recomputed from a source whenever that source changes. You can call set() or update() on it at any time. A manual write holds until the next source change.
The shorthand form
import { linkedSignal, signal } from '@angular/core';
const sizes = signal(['small', 'medium', 'large']);
const selected = linkedSignal(() => sizes()[0]);
console.log(selected()); // 'small'
selected.set('large'); // user override
console.log(selected()); // 'large'
sizes.set(['xs', 's', 'm']); // source changed
console.log(selected()); // 'xs'The function you pass works like a computed() body. Every signal it reads counts as a dependency. When any dependency changes, the next read of selected() reruns the function and discards the override. Like computed(), the recomputation is lazy: it runs on read, not at the moment the source changes.
The object form: keeping the previous value
A blind reset is often too blunt. If the user picked "medium" and the new options still include "medium", the selection should probably stay. The object form gives the computation access to the previous state:
import { Component, linkedSignal, signal } from '@angular/core';
interface Option { id: number; label: string; }
@Component({
selector: 'app-size-picker',
template: `
@for (opt of options(); track opt.id) {
<button (click)="selected.set(opt)">
{{ opt.id === selected().id ? '●' : '○' }} {{ opt.label }}
</button>
}
<button (click)="loadKids()">Kids sizes</button>
`,
})
export class SizePickerComponent {
options = signal<Option[]>([
{ id: 1, label: 'S' },
{ id: 2, label: 'M' },
{ id: 3, label: 'L' },
]);
selected = linkedSignal<Option[], Option>({
source: this.options,
computation: (newOptions, previous) =>
newOptions.find(o => o.id === previous?.value.id) ?? newOptions[0],
});
loadKids() {
this.options.set([
{ id: 2, label: 'M' },
{ id: 4, label: 'XS' },
]);
}
}source is the signal (or a function reading signals) that triggers recomputation. computation receives the new source value and a previous object with two fields: previous.source and previous.value. On the first run, previous is undefined, which is why the code uses ?.. Only source is tracked. Signals read inside computation are not dependencies.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Using `effect()` to copy a source into a `signal()` | Extra change cycles and glitchy intermediate values | Use `linkedSignal()` |
| Expecting an override to survive a source change | Shorthand form always resets | Use the object form and check `previous` |
| Forgetting `previous` is `undefined` initially | Runtime `TypeError` | Use optional chaining |
| Replacing the source with an equal-looking new array | Recomputes and resets anyway | Expected: arrays are compared by reference unless you pass `equal` |
Exercise
In the SizePickerComponent, the user selects "L" (id 3) and then clicks "Kids sizes". What does selected() return, and what would it return if they had selected "M" instead?
Show answer
With "L" selected, id 3 is not in the new list, so find() returns undefined and the computation falls back to newOptions[0], which is { id: 2, label: 'M' }. With "M" selected, id 2 is still present, so the selection stays on { id: 2, label: 'M' } (the new object from the new list).
Exercise
Why is linkedSignal() preferable to an effect() that calls selected.set(sizes()[0]) whenever sizes changes?
Show answer
The effect runs asynchronously after the change, so readers can briefly see a stale selection that does not match the new options. Writing signals from effects also adds extra change-detection work. linkedSignal recomputes synchronously and lazily when read, so it always matches its source.