Discover how a simple setting can make your Angular app feel lightning fast!
Why When to use OnPush in Angular? - Purpose & Use Cases
Imagine you have a big Angular app with many components, and every time something changes anywhere, Angular checks all components to see if they need updating.
This can slow down your app and make it feel laggy.
Without optimization, Angular runs change detection on every component all the time, even if nothing changed in some parts.
This wastes time and computer power, making your app slower and less smooth.
Using OnPush tells Angular to only check a component when its input data changes or an event happens inside it.
This way, Angular skips unnecessary checks and your app runs faster and smoother.
changeDetection: ChangeDetectionStrategy.Default
changeDetection: ChangeDetectionStrategy.OnPush
OnPush enables your app to run efficiently by reducing unnecessary work, making it faster and more responsive.
Think of a social media feed where only new posts or likes update the screen, instead of rechecking every post all the time.
Without OnPush, Angular checks all components often, slowing the app.
OnPush limits checks to when inputs change or events occur.
This improves app speed and user experience.
Practice
ChangeDetectionStrategy.OnPush in an Angular component?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 AQuick Check:
OnPush improves performance = A [OK]
- Thinking OnPush disables change detection
- Assuming OnPush checks on every event
- Believing OnPush updates without input changes
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 BQuick Check:
Use enum OnPush in decorator = D [OK]
- Using string 'OnPush' instead of enum
- Setting changeDetection to true or false
- Confusing Default with 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?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 DQuick Check:
OnPush needs new input references = A [OK]
- Assuming mutation triggers update
- Expecting runtime errors on mutation
- Thinking manual detection is always needed
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 CQuick Check:
New input references trigger OnPush updates = C [OK]
- Relying on manual detectChanges() calls unnecessarily
- Switching to Default without reason
- Removing @Input() breaks data flow
items.push(newItem). The child does not update. How should you fix this to ensure the child updates efficiently?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
Usingitems = [...items, newItem]creates a new array reference, triggering OnPush change detection in the child.Final Answer:
Useitems = [...items, newItem]to create a new array reference -> Option AQuick Check:
Immutable input updates trigger OnPush = B [OK]
- Mutating arrays with push expecting updates
- Relying on manual detectChanges() calls
- Changing strategy unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
