0
0
Angularframework~20 mins

TrackBy in ngFor in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TrackBy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does trackBy affect ngFor rendering?

Consider an Angular component that renders a list of items using ngFor. What is the main effect of adding a trackBy function?

Angular
<ul>
  <li *ngFor="let item of items; trackBy: trackById">{{ item.name }}</li>
</ul>

trackById(index: number, item: any) {
  return item.id;
}
AAngular reuses existing DOM elements for items with the same <code>id</code>, improving performance.
BAngular always recreates all DOM elements regardless of <code>trackBy</code>, so no performance change.
CAngular removes all items and then adds new ones, ignoring the <code>trackBy</code> function.
DAngular sorts the list automatically based on the <code>trackBy</code> return value.
Attempts:
2 left
💡 Hint

Think about how Angular decides which DOM elements to keep or recreate when the list changes.

📝 Syntax
intermediate
2:00remaining
Identify the correct trackBy function syntax

Which of the following trackBy function definitions is syntactically correct in Angular?

AtrackByFn: (index, item) => { item.id }
BtrackByFn(index, item) => item.id;
CtrackByFn(index: number, item: any) { return item.id; }
DtrackByFn(index: number, item: any) => { return item.id; }
Attempts:
2 left
💡 Hint

Remember the correct way to define a method in an Angular component class.

🔧 Debug
advanced
2:00remaining
Why does the trackBy function not prevent re-rendering?

Given this trackBy function, why does Angular still recreate all DOM elements when the list updates?

trackByFn(index: number, item: any) {
  return index;
}
ABecause returning <code>index</code> causes Angular to treat all items as new when the list order changes.
BBecause the function must return the entire <code>item</code> object, not just a property.
CBecause <code>trackBy</code> functions cannot use the <code>index</code> parameter.
DBecause the function is missing a return type declaration.
Attempts:
2 left
💡 Hint

Think about what happens if the list order changes but the trackBy returns the position.

state_output
advanced
2:00remaining
What is the rendered output after updating the list with trackBy?

Given this component code, what will be the content of the rendered list after updating items?

items = [
  { id: 1, name: 'Apple' },
  { id: 2, name: 'Banana' }
];

// Later update
this.items = [
  { id: 2, name: 'Banana' },
  { id: 1, name: 'Apple' }
];

trackByFn(index: number, item: any) { return item.id; }
Angular
<ul>
  <li *ngFor="let item of items; trackBy: trackByFn">{{ item.name }}</li>
</ul>
A<li>Banana</li><li>Banana</li>
B<li>Apple</li><li>Banana</li>
C<li>Apple</li><li>Apple</li>
D<li>Banana</li><li>Apple</li>
Attempts:
2 left
💡 Hint

Look at the updated items array order and how trackBy identifies items.

🧠 Conceptual
expert
3:00remaining
Why is trackBy important in large dynamic lists?

In an Angular app with a large list that updates frequently, why is using a proper trackBy function critical?

AIt automatically sorts the list items alphabetically to improve user experience.
BIt prevents Angular from recreating all DOM elements, reducing rendering time and improving app responsiveness.
CIt disables change detection for the list, so Angular never updates the DOM.
DIt caches the list data on the server to reduce network requests.
Attempts:
2 left
💡 Hint

Think about how Angular updates the DOM when lists change and the cost of recreating many elements.