Bird
0
0

You wrote this trackBy function but Angular is not updating the DOM efficiently. What is the error?

medium📝 Debug Q6 of 15
Angular - Performance Optimization
You wrote this trackBy function but Angular is not updating the DOM efficiently. What is the error?
trackByFn(index, item) {
  return index;
}

// items array is reordered but items have same content
AtrackBy function must return item object, not index
BUsing index as key causes wrong DOM updates on reorder
CtrackBy function should not have parameters
DtrackBy function must return a boolean
Step-by-Step Solution
Solution:
  1. Step 1: Analyze using index as trackBy key

    Using index means Angular tracks items by their position, not identity.
  2. Step 2: Effect on reordered list

    If items reorder but content is same, Angular thinks items changed because indexes changed, causing inefficient DOM updates.
  3. Final Answer:

    Using index as key causes wrong DOM updates on reorder -> Option B
  4. Quick Check:

    trackBy should use unique id, not index for reorder [OK]
Quick Trick: Avoid index as trackBy key if list can reorder [OK]
Common Mistakes:
  • Returning item object instead of unique id
  • Omitting parameters in trackBy function
  • Returning boolean instead of unique key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes