Since recently, the Angular styleguide-lint-extender "Codelyzer" is throwing warnings when you do not have a trackBy-function implemented on every *ngFor. I am wondering why this is considered an issue at all.
- In this blog the example of implementing trackBy comes down to
trackByFn(index, item) { return index;} // or item.id
. If I switched from index to item.id, how would this make my app faster? when it comes to array insertions or deletions, index is the most straightforward thing that matters. Why should the [ngFor]-directive compare object identity values instead? - in the module ng_for_of.d.ts I can find
_trackByFn
. So I assume, areturn index
-trackBy is the default configuration anyway? Then why is it considered a good practice to implement it explicitly?
Now personally, I do have a big collection (array) in my app, and it lies in a redux store. It can only be replaced by an empty array or new items can get added to it, eg:
return {...state, myArray: [...state.myArray, ...newItems]}
),
but never moved or deleted. Would it make sense for me to track by item.id
instead of index
? Should I really implement a return index;
-function in every component with an *ngFor?