简体   繁体   中英

UI flicker when array is changed angular 11

I am contacting an array on value updated by the server, but the UI template flicker on concat.. what should be done to avoid such a situation.

   @Input('companies')   set setCompanyArray(companies) {
     this.showNotFound = false;> 
     if (!companies) {
       return;
     }
     companies.map((company) => {
       company['searchFilter'] = this.lastSearchedText;
    });
    if (!this.companies) {
       this.companies = companies;
       return;
     }
     this.companies = this.companies.concat(companies);

Each time you assign a new value to companies variable, you will see flickering because Angular is rendering the whole list again. You should add trackByFn to tell Angular whether to rerender the whole list or just new items.

 <li *ngFor="let company of companies;trackBy: trackByFn"></li>

  trackByFn(index, company: Company) {

    return company.id // or whatever is an unique identificator for a company
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM