Sub topic 7
Immutable updates and input reference checks
This chapter assumes you know Angular components, input() bindings, and the difference between mutating an object and replacing it. It also assumes you have met the OnPush strategy from the previous chapter.
What OnPush compares
An OnPush component is skipped during a check unless something marks it dirty. One trigger is a change to an input. Angular decides that an input changed by comparing the old and new values with Object.is, which is a strict reference check for objects.
- For primitives such as strings and numbers, the check compares values.
'Ada'and'Ada'are equal. - For objects and arrays, the check compares references. Two objects with identical contents are different if they are separate objects.
So if a parent changes a property on an object and passes the same object again, the child sees the same reference and is not checked.
Mutation versus replacement
The child below is OnPush and reads one input.
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
@Component({
selector: 'app-user-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>{{ user().name }}</p>`,
})
export class UserCard {
user = input.required<{ name: string }>();
}The parent can update the user in two ways.
import { Component } from '@angular/core';
import { UserCard } from './user-card';
@Component({
selector: 'app-parent',
imports: [UserCard],
template: `
<app-user-card [user]="user" />
<button (click)="mutate()">Mutate</button>
<button (click)="replace()">Replace</button>
`,
})
export class Parent {
user = { name: 'Ada' };
mutate() {
this.user.name = 'Grace'; // same reference: the OnPush child is not updated
}
replace() {
this.user = { ...this.user, name: 'Grace' }; // new reference: the child is checked
}
}Clicking Mutate changes the data, but the binding [user]="user" still holds the same reference. Angular sees no input change, so the child's <p> keeps showing Ada. Clicking Replace creates a new object with the spread operator. The reference differs, Angular marks the child dirty, and the <p> shows Grace.
Immutable update patterns
An immutable update builds a new value and leaves the old one untouched. These are the common forms.
| Change | Mutating form (avoid) | Immutable form |
|---|---|---|
| Set a property | `user.name = 'Grace'` | `user = { ...user, name: 'Grace' }` |
| Add to an array | `items.push(x)` | `items = [...items, x]` |
| Remove from an array | `items.splice(i, 1)` | `items = items.filter((_, n) => n !== i)` |
| Update one element | `items[i].done = true` | `items = items.map((it, n) => n === i ? { ...it, done: true } : it)` |
The spread operator copies only one level. For nested data, copy each level on the path to the changed value. Objects you did not touch can keep their references, so unrelated OnPush children are still skipped.
Common errors
- Mutating, then expecting a refresh. The data changes but the view does not. Replace the object or array instead.
- Copying too often. Creating a new object on every call, for example in a getter or a template method, gives the child a new reference each time. The child then updates on every parent check, which cancels the benefit of OnPush.
- Shallow copy of nested data.
{ ...order }still sharesorder.address. If you mutateorder.address.city, a child that receivesorder.addressdoes not update. - Mutating inside the child. The child may show the new value after its own event fires, because events in its template mark it dirty. Other components holding the same object can still be stale.
Exercise
A parent holds items: string[] and passes it to an OnPush child with [items]="items". The parent runs this.items.push('new'), and the child's list does not change. Give a one-line fix and explain why it works.
Show answer
Use this.items = [...this.items, 'new'];. This creates a new array, so the input reference differs from the previous one. Angular detects the input change, marks the OnPush child dirty, and updates its view. push keeps the same array reference, so the child was skipped.