Search code examples
javascriptangularangular-directive

I have multiple ngFor in angular project use trackBy:trackByFunction with single function or create multiple function?


trackByFunction (index, items) {
   return item.id;

}

trackByABCtList(index, item) {
        return item.id; // or item.id
    }

trackByEmployeelist(index, item) {
    return item.id; // or item.id
}

trackByTags(index, item) {
    return item.id; // or item.id
}

trackByGroup(index, item) {
    return item.id; // or item.id
}

trackByEmployeesrange(index, item) {
    return item.id; // or item.id
}

trackByDepartment(index, item) {
    return item.id; // or item.id
}

trackByFeedbackQuestion(index, item) {
    return item.id; // or item.id
}

All Function same return type item.id, SO My Question is, I need to multiple trackByFunction or it's okay use only one.


Solution

  • It is ok to have one function. This functions is pure (e.g. no mutations) so it won't matter which *ngFor will be calling the function, so you can even move this function to some utils.ts file and name it something like trackById and use this function across your project.