OnPush change detection is easiest to explain as a real stale-UI debugging story, and since Angular 22 it is the default for every component that does not set a strategy. Angular skips an OnPush component unless a narrow trigger happens, so production bugs usually come from mutating references, updating state inside manual subscriptions, or misunderstanding parent-child boundaries. After the v22 upgrade the same bugs show up in the components that the Eager migration did not cover.
Frontend interview practice question
Angular OnPush by Default: Debug Stale UI After the v22 Upgrade
Interview quick answer
Interview focus
This Angular interview question tests whether you can explain Angular OnPush by Default: Debug Stale UI After v22, connect it to production trade-offs, and handle common follow-up questions.
- Angular OnPush by Default: Debug Stale UI After v22 explanation without falling back to memorized definitions
- Change Detection and Onpush reasoning, edge cases, and production failure modes
- How you would answer the most likely Angular interview follow-up
Use this Angular interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Full interview answer
Core ideaChangeDetectionStrategy.OnPush tells Angular: “Only check this component if something meaningful happened.” Meaningful = an @Input reference changed, an event happened in this component or its children, an async pipe emitted, a signal read by its template changed, or you manually asked Angular to check. Since Angular 22 this is also the default: a component without a changeDetection property is OnPush.
What happened? | Will OnPush re-render? | Why |
|---|---|---|
An @Input reference changed | ✅ Yes | Angular compares references, not deep values. |
A click/event inside the component | ✅ Yes | Events mark the component dirty. |
An async pipe emits a new value | ✅ Yes | AsyncPipe internally calls markForCheck(). |
You mutated an object/array in place | ❌ No | Reference did not change, Angular thinks nothing happened. |
You call markForCheck() manually | ✅ Yes | You explicitly told Angular to re-check. |
You call detectChanges() manually | ✅ Yes (immediate) | Forces synchronous change detection for this subtree. |
After the Angular 22 upgrade: where stale UI comes from
Angular 22 made OnPush the default, but an upgraded app does not change behavior on day one. ng update runs a migration that writes changeDetection: ChangeDetectionStrategy.Eager into every component that had no strategy and renames ChangeDetectionStrategy.Default to Eager. Component libraries compiled before v22 keep Eager semantics too, because the linker reads the Angular version each component was compiled with. Stale UI appears where neither safety net applies:
- New components.
ng generate componentin Angular CLI 22 writes nochangeDetectionline, so the component is OnPush. Pre-v22 habits such asthis.items.push(item)or assigning a field insidesubscribe()go stale right away. - Cleanup commits. Deleting the migrated
Eagerlines to adopt the new default turns every hidden mutation in those components into a stale-UI bug. - Classes the migration did not visit. In the 22.2 schematic source, the migration rewrites top-level component classes whose
@Component()call receives an inline object literal. A test host declared inside adescribe()callback, or a decorator that receives a shared metadata constant, keeps no strategy and becomes OnPush. - Upgrades that skipped
ng update. Bumping versions inpackage.jsonby hand runs no migration at all.
Run the trigger checklist below on those components first. The Eager vs OnPush visualizer replays the same bugs step by step.
Symptom after the upgrade | Likely cause | Fix |
|---|---|---|
A list misses the new row until something else is clicked. | The parent mutates the array with | Pass a new array, or keep the list in a signal and call |
A counter or total changes only after an unrelated click. | A | Render the Observable with the |
A child ignores a value set through | TypeScript code writes the child input directly, and Angular does not check an OnPush child for that write. | Bind the value in the parent template, or call |
A unit test fails because | The test writes a field on | Use |
The screen worked after the upgrade and broke in a later refactor. | An | Delete |
The classic real-world bug
“My data changes but the UI doesn’t update.” This almost always means you mutated an object or array instead of creating a new reference.
@Component({
selector: 'app-user-list',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div *ngFor="let user of users">{{ user.name }}</div>
`
})
export class UserListComponent {
@Input() users: { name: string }[] = [];
// ❌ BUG: UI will NOT update in OnPush
addUserWrong() {
this.users.push({ name: 'New User' }); // same array reference
}
// ✅ Correct: new reference => OnPush sees change
addUserCorrect() {
this.users = [...this.users, { name: 'New User' }];
}
}
Why OnPush exists
Eager change detection, called Default before Angular 22, checks every view the pass reaches, and in a Zone.js app a pass follows every async event (timers, HTTP, clicks, etc). OnPush lets Angular skip huge parts of the tree unless something relevant changed. This is critical for large apps and big lists.
Second real bug variant: manual subscription updates state, but the view still looks stale
Even if no input mutation happens, an OnPush component can still miss updates when you subscribe imperatively and update local state without async pipe or markForCheck(). This is the other classic production bug after in-place mutation.
@Component({
selector: 'app-summary-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `{{ stats.total }}`
})
export class SummaryCardComponent {
stats = { total: 0 };
constructor(
private statsService: StatsService,
private cd: ChangeDetectorRef
) {
this.statsService.total$.subscribe(total => {
// ❌ Bug: same object reference + no explicit mark
this.stats.total = total;
});
}
connectSafely() {
this.statsService.total$.subscribe(total => {
this.stats = { ...this.stats, total };
this.cd.markForCheck();
});
}
}
markForCheck() vs detectChanges()
Method | What it does | When to use |
|---|---|---|
| Marks this component and its parents as dirty for the next CD cycle. | When data changes outside Angular's awareness (e.g., custom subscriptions, timers, non-async-pipe streams). |
| Immediately runs change detection on this component subtree. | Rare cases: manual rendering, detached views, or very controlled performance scenarios. |
AsyncPipe is your best friend
The async pipe automatically:
• Subscribes
• Unsubscribes
• Calls markForCheck() on emission
This is why OnPush + async pipe is the recommended default pattern.
Common senior-level pitfalls
• Mutating arrays/objects instead of replacing them
• Forgetting that OnPush uses reference checks, not deep checks
• Updating state inside subscriptions without async pipe or markForCheck
• Assuming a parent mutating child.input.deep.nestedValue should refresh the child even though the input reference stayed the same
• Using OnPush everywhere without understanding its mental model
Scenario | What to do | Why |
|---|---|---|
State comes from Observables | Use | Automatic CD + auto unsubscribe + best performance. |
State updated manually in code | Use immutable updates (new reference) | So OnPush can detect the change. |
State updated outside Angular or in manual subscriptions | Call | Otherwise Angular will not re-render. |
How to debug it in a real app
Use Angular DevTools or a quick logging pass to verify which component is actually being checked. If the parent rerenders but the child does not, look for an unchanged input reference. If neither rerenders after a subscription callback, check whether the update happened outside the normal template flow and needs markForCheck() or an async pipe.
Copyable test: prove the stale row before you fix it
This spec reproduces the most common post-upgrade bug without a browser. The child sets no strategy, so on Angular 22 it is OnPush, and the host opts into Eager so the test isolates the child. The first test passes because the row stays stale, which is the proof. The second test shows the fix. On Angular 21 or earlier the first test fails, because the child would still be checked eagerly. Run it with the test runner your Angular CLI project already uses, Karma with Jasmine or Vitest.
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { TestBed } from '@angular/core/testing';
type User = { id: number; name: string };
@Component({
selector: 'app-user-list',
// No changeDetection here: OnPush by default since Angular 22.
template: `<ul>@for (user of users; track user.id) { <li>{{ user.name }}</li> }</ul>`,
})
class UserListComponent {
@Input() users: User[] = [];
}
@Component({
selector: 'app-user-list-host',
imports: [UserListComponent],
changeDetection: ChangeDetectionStrategy.Eager,
template: `<app-user-list [users]="users" />`,
})
class UserListHostComponent {
users: User[] = [{ id: 1, name: 'Ada' }];
}
describe('UserListComponent after the Angular 22 upgrade', () => {
function rowCount(element: HTMLElement): number {
return element.querySelectorAll('li').length;
}
it('stays stale when the parent mutates the same array', () => {
const fixture = TestBed.createComponent(UserListHostComponent);
fixture.detectChanges();
fixture.componentInstance.users.push({ id: 2, name: 'Linus' });
fixture.detectChanges();
expect(rowCount(fixture.nativeElement)).toBe(1);
});
it('renders the new row when the parent passes a new array', () => {
const fixture = TestBed.createComponent(UserListHostComponent);
fixture.detectChanges();
const host = fixture.componentInstance;
host.users = [...host.users, { id: 2, name: 'Linus' }];
fixture.detectChanges();
expect(rowCount(fixture.nativeElement)).toBe(2);
});
});
Run these bugs in the visualizer
Both stale-UI stories on this page are preset scenarios in the Angular change detection visualizer, which traces which of seven components Angular checks after each trigger, in Eager and OnPush and with or without Zone.js:
- Array push with an OnPush list: the same array reference, so UserList is skipped and the new row never renders.
- subscribe() without a mark: SummaryCard assigns a field in the callback and stays stale until the next click.
- Service timer with an OnPush footer: Zone.js runs the pass, but nothing marked LiveCount.
- Signal inside OnPush: one view refreshes while its OnPush ancestors are only traversed.
- Zoneless timer: no signal, event, async pipe, or markForCheck() ran, so no pass runs at all.
Interview-level summaryOnPush makes Angular check a component only when inputs change by reference, an event happens, an async pipe emits, or you manually trigger detection. It gives big performance wins, but requires immutable state updates. Most “UI not updating” bugs in OnPush come from mutating objects instead of replacing them. Since Angular 22 OnPush is the default, so the same bugs now appear in any component that sets no strategy and did not get an Eager line from the migration.
Use this as one explanation rep, then continue with the Angular interview questions cluster or a guided prep path.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.