Frontend interview practice question

Angular OnPush by Default: Debug Stale UI After the v22 Upgrade

HighIntermediateAngular

Interview quick answer

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.

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
Practice more Angular interview questions
Interview answer drill

Use this Angular interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

Core idea

ChangeDetectionStrategy.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.

What triggers change detection in OnPush components?

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 component in Angular CLI 22 writes no changeDetection line, so the component is OnPush. Pre-v22 habits such as this.items.push(item) or assigning a field inside subscribe() go stale right away.
  • Cleanup commits. Deleting the migrated Eager lines 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 a describe() callback, or a decorator that receives a shared metadata constant, keeps no strategy and becomes OnPush.
  • Upgrades that skipped ng update. Bumping versions in package.json by 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 push() or splice(), so the input reference stays the same.

Pass a new array, or keep the list in a signal and call update() with a new array.

A counter or total changes only after an unrelated click.

A subscribe(), timer, or WebSocket callback assigns a plain field.

Render the Observable with the async pipe, store the value in a signal, or call markForCheck().

A child ignores a value set through @ViewChild.

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 markForCheck() in the child.

A unit test fails because fixture.detectChanges() no longer shows the new value.

The test writes a field on fixture.componentInstance of an OnPush component, which marks nothing dirty.

Use fixture.componentRef.setInput() for inputs, drive state through signals or events, or give the test host ChangeDetectionStrategy.Eager.

The screen worked after the upgrade and broke in a later refactor.

An Eager line written by the migration was deleted while the component still mutates state.

Delete Eager only after the component reads its state from inputs, signals, or the async pipe.

Post-upgrade stale UI checklist

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.

TYPESCRIPT
@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.

TYPESCRIPT
@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

markForCheck()

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).

detectChanges()

Immediately runs change detection on this component subtree.

Rare cases: manual rendering, detached views, or very controlled performance scenarios.

Manual change detection tools

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 async pipe

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 markForCheck()

Otherwise Angular will not re-render.

Correct patterns with OnPush

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.

TYPESCRIPT
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:

Interview-level summary

OnPush 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.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub