Bird
Raised Fist0
Angularframework~10 mins

When to use OnPush in Angular - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the change detection strategy to OnPush in an Angular component.

Angular
import { Component, ChangeDetectionStrategy } from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `<p>Hello OnPush!</p>`,
  changeDetection: ChangeDetectionStrategy.[1]
})
export class SampleComponent {}
Drag options to blanks, or click blank then click option'
A'OnPush'
BDefault
CCheckAlways
DOnPush
Attempts:
3 left
💡 Hint
Common Mistakes
Using Default instead of OnPush
Using string 'OnPush' instead of the enum member
Using a non-existent strategy name
2fill in blank
medium

Complete the code to import the correct Angular symbol needed to use OnPush strategy.

Angular
import { Component, [1] } from '@angular/core';
Drag options to blanks, or click blank then click option'
AChangeDetectorRef
BChangeDetectionStrategy
CNgZone
DRenderer2
Attempts:
3 left
💡 Hint
Common Mistakes
Importing ChangeDetectorRef instead of ChangeDetectionStrategy
Importing unrelated Angular symbols
3fill in blank
hard

Fix the error in the component decorator to correctly use OnPush change detection.

Angular
@Component({
  selector: 'app-test',
  template: `<p>Test</p>`,
  changeDetection: [1]
})
export class TestComponent {}
Drag options to blanks, or click blank then click option'
AChangeDetectionStrategy.OnPush
BChangeDetectionStrategy.onpush
CChangeDetection.OnPush
DOnPush
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'OnPush' without the enum
Wrong casing in 'onpush'
Using a non-existent enum name
4fill in blank
hard

Fill both blanks to create a component with OnPush strategy and a simple input property.

Angular
import { Component, Input, [1] } from '@angular/core';

@Component({
  selector: 'app-item',
  template: `<p>{{data}}</p>`,
  changeDetection: [2]
})
export class ItemComponent {
  @Input() data!: string;
}
Drag options to blanks, or click blank then click option'
AChangeDetectionStrategy
BChangeDetectorRef
CChangeDetectionStrategy.OnPush
DChangeDetectionStrategy.Default
Attempts:
3 left
💡 Hint
Common Mistakes
Importing ChangeDetectorRef instead of ChangeDetectionStrategy
Using Default instead of OnPush in the decorator
5fill in blank
hard

Fill all three blanks to create a component with OnPush strategy, an input property, and a method that triggers change detection manually.

Angular
import { Component, Input, [1], [2] } from '@angular/core';

@Component({
  selector: 'app-detail',
  template: `<p>{{info}}</p>`,
  changeDetection: [3]
})
export class DetailComponent {
  @Input() info!: string;

  constructor(private cd: [2]) {}

  refresh() {
    this.cd.detectChanges();
  }
}
Drag options to blanks, or click blank then click option'
AChangeDetectionStrategy
BChangeDetectorRef
CChangeDetectionStrategy.OnPush
DNgZone
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing ChangeDetectorRef
Using Default strategy instead of OnPush
Injecting wrong service in constructor

Practice

(1/5)
1. What is the main reason to use ChangeDetectionStrategy.OnPush in an Angular component?
easy
A. To improve performance by checking for changes only when inputs change or events occur
B. To disable change detection completely
C. To force Angular to check for changes on every event
D. To automatically update the component without any input changes

Solution

  1. Step 1: Understand OnPush behavior

    OnPush tells Angular to check for changes only when input properties change or events inside the component happen.
  2. Step 2: Compare with other options

    It does not disable change detection or force checks on every event, but optimizes performance by limiting checks.
  3. Final Answer:

    To improve performance by checking for changes only when inputs change or events occur -> Option A
  4. Quick Check:

    OnPush improves performance = A [OK]
Hint: OnPush triggers checks only on input or event changes [OK]
Common Mistakes:
  • Thinking OnPush disables change detection
  • Assuming OnPush checks on every event
  • Believing OnPush updates without input changes
2. Which of the following is the correct way to set OnPush change detection in an Angular component?
easy
A. @Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.Default })
B. @Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush })
C. @Component({ selector: 'app-example', changeDetection: 'OnPush' })
D. @Component({ selector: 'app-example', changeDetection: true })

Solution

  1. Step 1: Recall Angular syntax for change detection

    The correct syntax uses the enum ChangeDetectionStrategy.OnPush inside the component decorator.
  2. Step 2: Eliminate incorrect options

    A uses Default strategy, B uses a boolean which is invalid, C uses a string instead of enum.
  3. Final Answer:

    @Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush }) -> Option B
  4. Quick Check:

    Use enum OnPush in decorator = D [OK]
Hint: Use ChangeDetectionStrategy.OnPush enum in @Component [OK]
Common Mistakes:
  • Using string 'OnPush' instead of enum
  • Setting changeDetection to true or false
  • Confusing Default with OnPush
3. Given this component using OnPush:
@Component({
  selector: 'app-counter',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `{{count.value}}`
})
export class CounterComponent {
  @Input() count = {value: 0};
}

What happens if the parent updates count by mutating an object property instead of replacing the input?
medium
A. The component updates only after a manual change detection call
B. The component updates and shows the new count
C. Angular throws a runtime error
D. The component does not update because OnPush requires immutable input changes

Solution

  1. Step 1: Understand OnPush input change detection

    OnPush detects changes only when input references change (immutable updates), not when internal properties mutate.
  2. Step 2: Analyze mutation effect

    Mutating an object property keeps the same reference, so Angular does not detect the change and does not update the view.
  3. Final Answer:

    The component does not update because OnPush requires immutable input changes -> Option D
  4. Quick Check:

    OnPush needs new input references = A [OK]
Hint: OnPush needs new input objects, not mutated ones [OK]
Common Mistakes:
  • Assuming mutation triggers update
  • Expecting runtime errors on mutation
  • Thinking manual detection is always needed
4. You have an Angular component with OnPush strategy but the view does not update after input changes. Which fix will solve this problem?
medium
A. Manually call ChangeDetectorRef.detectChanges() after input mutation
B. Switch to Default change detection strategy
C. Replace the input object with a new reference instead of mutating it
D. Remove the @Input() decorator

Solution

  1. Step 1: Identify why view doesn't update

    OnPush requires new input references to detect changes; mutating inputs keeps the same reference, so no update.
  2. Step 2: Choose the best fix

    Replacing the input with a new object triggers change detection properly without needing manual calls or strategy changes.
  3. Final Answer:

    Replace the input object with a new reference instead of mutating it -> Option C
  4. Quick Check:

    New input references trigger OnPush updates = C [OK]
Hint: Use new input objects, not mutations, to update OnPush views [OK]
Common Mistakes:
  • Relying on manual detectChanges() calls unnecessarily
  • Switching to Default without reason
  • Removing @Input() breaks data flow
5. You have a parent component passing a list of items to a child component with OnPush strategy. The parent updates the list by adding a new item using items.push(newItem). The child does not update. How should you fix this to ensure the child updates efficiently?
hard
A. Use items = [...items, newItem] to create a new array reference
B. Call markForCheck() on the child's ChangeDetectorRef after push
C. Change child's strategy to Default
D. Manually trigger detectChanges() in the child after push

Solution

  1. Step 1: Understand OnPush and input references

    OnPush detects changes only when input references change, so mutating the array with push keeps the same reference.
  2. Step 2: Apply immutable update

    Using items = [...items, newItem] creates a new array reference, triggering OnPush change detection in the child.
  3. Final Answer:

    Use items = [...items, newItem] to create a new array reference -> Option A
  4. Quick Check:

    Immutable input updates trigger OnPush = B [OK]
Hint: Create new input arrays to trigger OnPush updates [OK]
Common Mistakes:
  • Mutating arrays with push expecting updates
  • Relying on manual detectChanges() calls
  • Changing strategy unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard