DEV Community

Cover image for ๐Ÿ” Angular Challenge #1: Understanding OnPush Change Detection in Angular
Rohtash Sethi for NG DELHI

Posted on

๐Ÿ” Angular Challenge #1: Understanding OnPush Change Detection in Angular

Welcome to the Angular Daily Challenge Series, where we decode real-world Angular concepts through fun and practical coding puzzles. This article dives into a common interview-level topic: OnPush change detection in Angular.

Let's analyze today's challenge and uncover the behavior of ChangeDetectorRef and OnPush.


โ“ Angular OnPush Change Detection Challenge

Here's the scenario:

@Component({
  selector: 'app-child',
  template: `
    <h1>Child Component</h1>
    <span>{{ data().text }}</span>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ChildComponent {
  data = input.required<{ text: string }>();
}

@Component({
  template: `
    <h1>Parent Component</h1>
    <app-child [data]="data" />
  `,
})
export class ChangeDetectionComponent {
  #cdRef = inject(ChangeDetectorRef);
  data = { text: 'Hello from Parent!' };

  constructor() {
    setTimeout(() => {
      this.data.text = 'Updated from Parent!';
      // Ensure the change is visible in the OnPush child
      this.#cdRef.detectChanges();
    }, 3_000);
  }
}
Enter fullscreen mode Exit fullscreen mode

โœ… What Will Be Displayed in the Child After 3 Seconds?

โœ… Output: "Updated from Parent!"

But why does this happen? Let's break it down with a deeper look into Angular's change detection system.


๐Ÿง  How Angular OnPush Works

When you use ChangeDetectionStrategy.OnPush, Angular skips checking the componentโ€™s template unless one of these occurs:

  • The component receives a new reference via input signal
  • An event happens inside the component
  • Change detection is manually triggered using ChangeDetectorRef

๐Ÿ”„ Why Mutation Doesnโ€™t Trigger OnPush Detection

In our example, we changed:

this.data.text = 'Updated from Parent!';
Enter fullscreen mode Exit fullscreen mode

This modifies a property of the object but doesn't change the object reference, so Angular doesn't detect this change on its own.

Hence, Angular won't rerender the child unless we force it using:

this.#cdRef.detectChanges();

Enter fullscreen mode Exit fullscreen mode

This is how we manually notify Angular to run change detection for all components, including OnPush ones.


๐Ÿ’ก Bonus Tip: 3_000 vs 3000 in JavaScript

You might have spotted:

setTimeout(() => { ... }, 3_000);

Enter fullscreen mode Exit fullscreen mode

This is JavaScript numeric separator syntax introduced in ES2021. Itโ€™s functionally identical to 3000, but more readable.

โœ… Benefits:

  • Easier to read large numbers (1_000_000, 3_000)
  • Especially useful for time intervals, byte sizes, and money values

โœ… Use it in modern Angular projects, it improves code clarity without affecting performance.


๐Ÿ” Learnings Recap

Concept Description
ChangeDetectionStrategy.OnPush Optimizes performance by skipping change detection unless needed
ChangeDetectorRef.detectChanges() Manually triggers Angularโ€™s change detection
Mutating object properties Doesnโ€™t trigger OnPush if the reference is unchanged
3_000 in setTimeout JavaScript numeric separator for better readability

๐Ÿ“ฃ Challenge for You: Can You Optimize This?

In this challenge, we manually triggered change detection. But is that always the best approach?

๐Ÿ” Explore These Alternatives:

  • โœ… Use immutable patterns: Replace the object entirely using this.data = { text: 'Updated' }
  • โšก Adopt Angular Signals (Angular 17+): Better reactivity with built-in efficiency
  • ๐Ÿง  Try markForCheck() instead of detectChanges() in some cases

๐Ÿ’ฌ Whatโ€™s your go-to strategy for handling OnPush changes? Drop your ideas and code snippets in the comments!


๐Ÿ™Œ Stay Connected

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Follow Rohtash Sethi ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿฅท๐ŸŒŸ for daily Angular challenges, JavaScript insights, and performance tips!

Top comments (1)

Collapse
 
ghost_engineer_2883a1ca0a profile image
Ghost Engineer โ€ข

try this if you get stuck during the interview. its an AI co-pilot that solves the questions for you so you can focus on the more important part of the interview, the communication part. its also a really good study tool: ghostengineer.com__

close