Why is track important in @for (or trackBy in *ngFor)?

Angular performance and SSR · Freshers

Model answer

When a list changes, Angular needs to know which items are the same so it can reuse their DOM elements instead of destroying and recreating all of them. The track expression gives each item a stable identity, usually its id: @for (user of users; track user.id).

Without a good track value, a refreshed list can lose focus, scroll position and input state, and can be slow. Never track by index for lists that can be reordered or filtered, unless the items truly have no id.

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions