Sub topic 6
OnPush: skipping subtrees that cannot have changed
This chapter assumes you know Angular components, inputs and outputs, template binding syntax, and how the Default strategy checks every component on each cycle. It covers the OnPush strategy and what makes an OnPush component get checked.
What OnPush changes
With the Default strategy, Angular checks every component in the tree on every change detection cycle. With OnPush, Angular skips a component and everything below it unless something marks that component as needing a check. Skipping a subtree means that none of its template bindings are compared.
You opt in per component:
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 setting applies to the component and its descendants in the view tree. A Default child inside an OnPush parent is still skipped whenever the parent is skipped.
When an OnPush component is checked
An OnPush component is checked only in these cases:
- An input binding receives a new reference. Angular compares the old and new values with
Object.is. - An event fires from the component's template or one of its child views, for example a
(click)handler. - The component is marked dirty with
ChangeDetectorRef.markForCheck(). The async pipe does this for you. - A signal read in the component's template changes.
Anything else, such as a setTimeout callback or a promise that resolves, does not mark the component dirty, even though Zone.js still starts a cycle.
Mutation versus replacement
The input check compares references, not contents. If a parent changes a property on an object it already passed down, the reference stays the same and the OnPush child does not see a new input.
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 card still shows "Ada". Clicking Replace creates a new object, so the card updates. This is why OnPush is usually paired with immutable updates: create a new object or array instead of editing the old one.
Updating from outside Angular's events
Timers, WebSocket messages and similar callbacks do not mark an OnPush component dirty. If one of them changes a plain field, call markForCheck():
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject } from '@angular/core';
@Component({
selector: 'app-ticker',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ count }}</p>`,
})
export class Ticker {
private cdr = inject(ChangeDetectorRef);
count = 0;
constructor() {
setInterval(() => {
this.count++;
this.cdr.markForCheck();
}, 1000);
}
}markForCheck() marks this component and all its ancestors as dirty. Angular then checks them in the next cycle. It does not run a cycle itself. Zone.js starts the cycle when the timer callback finishes.
Signals avoid the manual call. Angular tracks which component reads a signal in its template, so setting the signal schedules a check of that component:
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<button (click)="increment()">Clicked {{ count() }} times</button>`,
})
export class Counter {
count = signal(0);
increment() {
this.count.update((c) => c + 1);
}
}Common errors
- Mutating an input object or array. The view looks stale. Replace the value with a copy, using spread syntax or
mapandfilter. - Changing a plain field in an async callback. The field changes but the view does not. Use a signal, the async pipe, or
markForCheck(). - Assuming a Default child is always checked. If its OnPush ancestor is skipped, the child is skipped too.
Exercise
A parent holds items = ['a', 'b'] and passes it to an OnPush child with [items]="items". A button calls this.items.push('c'). The child's view does not change. Why, and what is a fix?
Show answer
push mutates the same array, so the input reference is unchanged and the OnPush child is not checked. Assign a new array instead, for example this.items = [...this.items, 'c'].