OnPush change detection

Intermediate · 17 min

Opting a component out of the default full-tree check, and the immutability discipline that makes it safe.

Before thisChange detection and zone.jsSignals, computed values, and effects

Decoded Frontend · 17 min

OnPush change detection

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Decoded FrontendReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ChangeDetectionStrategy.OnPush skips a component (and its subtree) during a change-detection pass unless one of a small set of specific triggers fired — a changed @Input() reference chief among them.
  • OnPush only sees a new @Input() when the reference changes — mutating an object or array in place, without replacing it, is invisible to an OnPush component.
  • Signals bypass this limitation entirely: a signal read inside an OnPush component's template notifies Angular directly when it changes, with no reliance on input-reference comparison at all.

Opting out of the default’s generosity

The previous lesson ended on a genuine cost: Angular’s default change-detection strategy re-checks the entire component tree on every async notification, whether or not a given component could plausibly have changed. OnPush is how an individual component opts out of being swept up in that by default:

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  // ...
})
export class UserCardComponent {
  @Input() user!: User;
}

With OnPush set, Angular skips this component (and everything nested inside it) during a change-detection pass, unless one of a specific, narrow set of things happened: one of its @Input() bindings received a new reference, an event originated from inside the component’s own template, an async-piped observable emitted, or — the case the next paragraph covers — a signal read in its template changed. Anything outside that list, and OnPush trusts that nothing relevant changed, and skips the check entirely.

Why the reference comparison is the sharp edge

That “new reference” condition is the part that trips people up. OnPush doesn’t deep-compare an input’s contents before and after — it checks whether the reference itself changed:

// invisible to OnPush — same array reference, just mutated
this.items.push(newItem);

// visible to OnPush — a genuinely new reference
this.items = [...this.items, newItem];

Mutating an object or array in place leaves its reference exactly as it was, so an OnPush component bound to that object has no way to tell anything happened — from its perspective, nothing changed, because nothing about the reference did. This is why OnPush comes bundled with an implicit discipline: immutability. Treat your data as something you replace, not mutate, and the reference-comparison check becomes a reliable signal instead of a trap; keep mutating in place out of habit, and OnPush components will quietly show stale data with no error anywhere to point you at why.

Where signals sidestep the whole problem

This is exactly the gap a signal read directly in a template closes. A signal doesn’t rely on comparing an input’s before-and-after reference at all — it notifies Angular the moment its own value changes, regardless of whether that value arrived through a prop, was computed internally, or came from an effect. An OnPush component that reads count() straight in its template gets re-checked when count changes, full stop, with no reference-equality discipline required to make that reliable. This is a meaningful part of why signals and OnPush pair so naturally — signals remove the one sharp edge OnPush has always carried.

This lesson’s video walks through OnPush’s behavior and the mutation trap with a concrete, runnable example — watch closely for the moment a .push() call fails to update the view, since seeing that failure directly is far more convincing than being told about it.

What’s next

The logical endpoint of this whole chapter is removing zone.js’s blanket notifications altogether, once signals make Angular’s own knowledge of “what changed” precise enough that the conservative, check-everything fallback is no longer needed. That’s the next lesson.

Check yourself

  1. 01An OnPush component receives `[items]="itemList"` as an input, and elsewhere `itemList.push(newItem)` runs. Does the OnPush component's view update?

    No — `.push()` mutates the existing array in place, so `itemList`'s reference never changes. OnPush only re-checks the component when the *reference* passed to an input changes, so a mutation that leaves the same object in place is invisible to it. Replacing the array (`this.itemList = [...this.itemList, newItem]`) would trigger the check, because that assigns a genuinely new reference.

  2. 02Why does a signal read directly inside an OnPush component's template avoid the stale-view problem entirely?

    Because a signal notifies Angular directly when its value changes — it doesn't rely on an input-reference comparison at all. An OnPush component reading `count()` in its template gets re-checked when `count` changes, regardless of whether that change arrived via a new input reference or an internal update, because the signal itself is the notification mechanism, not a before/after reference diff.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark