What does the OnPush change detection strategy do in Angular?
OnPush tells Angular to check a component only when its input properties change or when an event happens inside it, improving performance by skipping unnecessary checks.
Click to reveal answer
beginner
When should you consider using OnPush in your Angular components?
Use OnPush when your component's inputs are immutable or rarely change, and you want to optimize performance by reducing how often Angular checks for changes.
Click to reveal answer
intermediate
How does OnPush affect Angular's change detection compared to the default strategy?
With OnPush, Angular skips checking the component unless input references change or an event occurs inside it, unlike the default strategy which checks every component every time.
Click to reveal answer
intermediate
What kind of data changes are detected by OnPush strategy?
OnPush detects changes when input properties receive new object references or primitive values change, but not when internal mutable data changes without new references.
Click to reveal answer
intermediate
Why is immutability important when using OnPush?
Because OnPush relies on detecting new references to inputs, using immutable data ensures Angular notices changes and updates the component correctly.
Click to reveal answer
What triggers Angular to check a component with OnPush strategy?
AWhen input properties change by reference or an event occurs inside the component
BEvery time any component changes in the app
COnly when the user clicks a button
DNever, OnPush disables change detection
✗ Incorrect
OnPush triggers change detection only when input references change or an event happens inside the component.
Which scenario is best suited for OnPush strategy?
AComponent with immutable inputs that rarely change
BComponent with many internal mutable state changes
CComponent that updates every millisecond
DComponent with no inputs
✗ Incorrect
OnPush works best when inputs are immutable and changes are infrequent, improving performance.
What happens if you mutate an object input without changing its reference in an OnPush component?
AAngular will throw an error
BAngular will detect the change and update the view
CAngular will NOT detect the change and will NOT update the view
DAngular will reload the entire app
✗ Incorrect
OnPush detects changes by reference, so mutating without changing reference is not detected.
How can you manually trigger change detection in an OnPush component?
ABy adding more inputs
BBy refreshing the browser
CBy changing the component's selector
DBy injecting ChangeDetectorRef and calling markForCheck() or detectChanges()
✗ Incorrect
You can manually trigger detection using ChangeDetectorRef methods in OnPush components.
What is a main benefit of using OnPush strategy?
ADisables Angular's change detection
BImproved performance by reducing unnecessary checks
CAutomatic detection of all changes
DSimpler code with no inputs
✗ Incorrect
OnPush improves performance by limiting when Angular checks the component.
Explain when and why you would use the OnPush change detection strategy in Angular.
Think about how Angular decides to update components and how OnPush changes that.
You got /4 concepts.
Describe how OnPush affects Angular's change detection compared to the default strategy.
Compare the frequency and triggers of change detection in both strategies.
You got /4 concepts.
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
Step 1: Understand OnPush behavior
OnPush tells Angular to check for changes only when input properties change or events inside the component happen.
Step 2: Compare with other options
It does not disable change detection or force checks on every event, but optimizes performance by limiting checks.
Final Answer:
To improve performance by checking for changes only when inputs change or events occur -> Option A
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
Step 1: Recall Angular syntax for change detection
The correct syntax uses the enum ChangeDetectionStrategy.OnPush inside the component decorator.
Step 2: Eliminate incorrect options
A uses Default strategy, B uses a boolean which is invalid, C uses a string instead of enum.
Final Answer:
@Component({ selector: 'app-example', changeDetection: ChangeDetectionStrategy.OnPush }) -> Option B
Quick Check:
Use enum OnPush in decorator = D [OK]
Hint: Use ChangeDetectionStrategy.OnPush enum in @Component [OK]
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
Step 1: Understand OnPush input change detection
OnPush detects changes only when input references change (immutable updates), not when internal properties mutate.
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.
Final Answer:
The component does not update because OnPush requires immutable input changes -> Option D
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
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.
Step 2: Choose the best fix
Replacing the input with a new object triggers change detection properly without needing manual calls or strategy changes.
Final Answer:
Replace the input object with a new reference instead of mutating it -> Option C
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
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.
Step 2: Apply immutable update
Using items = [...items, newItem] creates a new array reference, triggering OnPush change detection in the child.
Final Answer:
Use items = [...items, newItem] to create a new array reference -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.