OnPush helps Angular check for changes only when needed. This makes apps faster by avoiding unnecessary work.
OnPush change detection strategy in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', changeDetection: ChangeDetectionStrategy.OnPush }) export class ExampleComponent { }
The changeDetection property is set inside the @Component decorator.
Use ChangeDetectionStrategy.OnPush to enable this strategy.
data changes.import { Component, Input, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-simple', template: `<p>{{ data }}</p>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class SimpleComponent { @Input() data!: string; }
setTimeout callback does not trigger change detection in OnPush unless inputs change or events occur.import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-counter', template: `<p>Count: {{ count }}</p>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class CounterComponent { count = 0; constructor() { setTimeout(() => { this.count++; }, 1000); } }
This component only updates its displayed message when the input message changes. Angular skips checking it otherwise, improving performance.
import { Component, Input, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-message', template: `<p>{{ message }}</p>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class MessageComponent { @Input() message!: string; } // Usage in parent component template: // <app-message [message]="parentMessage"></app-message> // When 'parentMessage' changes, MessageComponent updates. Otherwise, it does not check for changes.
OnPush works best when inputs are immutable or replaced with new objects.
Internal changes inside the component without input changes will NOT trigger view updates.
You can manually trigger change detection if needed using ChangeDetectorRef.
OnPush makes Angular check for changes only when inputs change or events happen.
This improves app speed by reducing unnecessary checks.
Use OnPush when your component depends mainly on inputs and does not change data internally.
Practice
OnPush change detection strategy do in Angular?Solution
Step 1: Understand OnPush behavior
OnPush strategy tells Angular to run change detection only when input properties change or events happen inside the component.Step 2: Compare with other options
Options A, C, and D describe behaviors that are not how OnPush works. It does not disable detection, nor does it check on every event or require manual triggers.Final Answer:
It checks for changes only when input properties change or events occur. -> Option BQuick Check:
OnPush triggers on input or event changes = B [OK]
- Thinking OnPush disables change detection
- Believing OnPush checks on every event
- Assuming manual triggers are required
OnPush change detection strategy in an Angular component?Solution
Step 1: Recall Angular syntax for change detection
The correct property name ischangeDetectionand the value isChangeDetectionStrategy.OnPush.Step 2: Analyze options
@Component({ selector: 'app', changeDetection: ChangeDetectionStrategy.OnPush }) uses the correct property and enum value. @Component({ selector: 'app', changeDetection: ChangeDetectionStrategy.Default }) uses Default strategy, C uses a string instead of enum, and D uses a wrong property name.Final Answer:
@Component({ selector: 'app', changeDetection: ChangeDetectionStrategy.OnPush }) -> Option AQuick Check:
Use changeDetection with enum OnPush = A [OK]
- Using string 'OnPush' instead of enum
- Wrong property name like changeDetectionStrategy
- Confusing Default with OnPush
OnPush strategy:@Component({
selector: 'counter',
template: `{{count}}`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent {
@Input() count = 0;
}
If the parent updates
count from 0 to 1, what will happen?Solution
Step 1: Understand OnPush input update behavior
OnPush triggers change detection when input properties change, so updatingcountfrom parent triggers view update.Step 2: Evaluate other options
The component view does not update until manually triggered. is wrong because manual trigger is not needed for input changes. The component throws an error because OnPush disallows input changes. is false; OnPush allows input changes. The component updates only if an event inside it triggers change detection. is incomplete because input changes alone trigger detection.Final Answer:
The component view updates to show 1 immediately. -> Option CQuick Check:
Input change triggers OnPush update = D [OK]
- Thinking manual detection is needed for input changes
- Believing OnPush blocks input updates
- Confusing event triggers with input triggers
OnPush strategy. Inside it, you update a local variable without changing any input. The view does not update. What is the likely cause?Solution
Step 1: Recall OnPush detection triggers
OnPush triggers change detection only on input changes or events inside the component.Step 2: Understand local variable update effect
Updating a local variable without input change or event does not trigger detection, so view stays the same.Step 3: Analyze other options
OnPush disables all change detection, so no updates happen. is false; OnPush does not disable detection completely. You must calldetectChanges()manually for every change. is sometimes needed but not always. The component must use Default strategy to update local variables. is incorrect; Default strategy is not required for local updates.Final Answer:
OnPush only checks for input changes or events, so local changes don't trigger detection. -> Option DQuick Check:
Local changes need input/event to trigger OnPush = A [OK]
- Assuming OnPush disables all detection
- Forgetting to trigger detection manually when needed
- Thinking Default strategy is required for local updates
OnPush strategy. The parent updates a property inside the object without changing the object reference. Will the child detect the change and update its view?Solution
Step 1: Understand OnPush input change detection
OnPush detects changes by checking if the input reference changes, not deep object mutations.Step 2: Analyze object mutation effect
Changing a property inside the object without changing the reference does not trigger OnPush detection.Step 3: Evaluate other options
Yes, because OnPush detects deep changes inside objects automatically. is false because OnPush does not do deep checks. Yes, but only if you callmarkForCheck()manually. is partially true but not automatic. No, because OnPush disables all change detection for objects. is incorrect; OnPush does not disable detection for objects.Final Answer:
No, because OnPush only detects changes when the input reference changes. -> Option AQuick Check:
OnPush detects input reference changes only = C [OK]
- Assuming deep object changes trigger OnPush
- Thinking OnPush disables detection for objects
- Forgetting to update object reference to trigger detection
