📖 Scenario: You are building a simple Angular app that shows a list of tasks. Each task has a name and a status. You want to make the app faster by telling Angular when to update the task list component.
🎯 Goal: Learn how to use the OnPush change detection strategy in Angular to improve performance by updating the component only when its input data changes.
📋 What You'll Learn
Create a standalone Angular component called TaskListComponent that displays a list of tasks.
Add a configuration variable to set the change detection strategy to OnPush.
Use an @Input() property called tasks to receive the list of tasks.
Add a button in the parent component to update the tasks list immutably and see the effect of OnPush.
💡 Why This Matters
🌍 Real World
Using <code>OnPush</code> helps Angular apps run faster by reducing unnecessary updates. This is important in apps with many components or complex UI.
💼 Career
Understanding change detection strategies like <code>OnPush</code> is key for Angular developers to write efficient, scalable applications.
Progress0 / 4 steps
1
Create the TaskListComponent with tasks input
Create a standalone Angular component called TaskListComponent with an @Input() property named tasks of type string[]. The component template should display each task inside an unordered list <ul> with list items <li>.
Angular
Hint
Use @Input() to receive data from the parent. Use *ngFor to loop over tasks in the template.
2
Set the change detection strategy to OnPush
Add the changeDetection property to the @Component decorator and set it to ChangeDetectionStrategy.OnPush. Import ChangeDetectionStrategy from @angular/core.
Angular
Hint
Import ChangeDetectionStrategy and add changeDetection: ChangeDetectionStrategy.OnPush inside the @Component decorator.
3
Use the TaskListComponent in a parent component
Create a parent component called AppComponent that uses <app-task-list> in its template. Define a tasks array property with these exact values: ['Wash dishes', 'Do laundry', 'Buy groceries']. Pass this array to app-task-list using property binding [tasks].
Angular
Hint
Use property binding [tasks] to pass the tasks array to the child component.
4
Add a button to update tasks immutably
In AppComponent template, add a button with text Complete first task. When clicked, update the tasks array by creating a new array that removes the first task. Use the spread operator to create the new array. This shows how OnPush detects changes only when the input reference changes.
Angular
Hint
Use the spread operator and slice(1) to create a new array without the first task.
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.