Bird
0
0

What is wrong with this ngOnChanges implementation?

medium📝 Debug Q7 of 15
Angular - Lifecycle Hooks
What is wrong with this ngOnChanges implementation?
ngOnChanges(changes: SimpleChanges): void {
  if (changes['inputProp'].currentValue) {
    console.log('Input changed');
  }
}
AIt does not check if <code>inputProp</code> actually changed
BIt should return a value instead of void
CIt should use <code>changes.inputProp.previousValue</code> instead
DIt should not use <code>SimpleChanges</code> as parameter
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition used

    The code checks if currentValue is truthy, not if the value changed.

  2. Step 2: Understand proper change detection

    It should check if previousValue !== currentValue to confirm a change.

  3. Final Answer:

    It does not check if inputProp actually changed -> Option A
  4. Quick Check:

    Check value difference, not just truthiness [OK]
Quick Trick: Compare previous and current values to detect real changes [OK]
Common Mistakes:
  • Checking only currentValue truthiness
  • Ignoring previousValue comparison
  • Misusing parameter types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes