question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Virtual-scroll not updating when initialized with empty data

See original GitHub issue

Bug Report

Ionic version:

[x] 4.1.1

Current behavior: Isssue is similar to #11532 On first user connect I download a collection of items from a database, I created an observable that next each item once response is given by database and push it to the data array. If I use a simple *ngFor, when the list is updated by the subscriber, item is drawn immediately. With virtual-scroll, it works if the data array is given already with some element, but not if it is empty. I use tabs to change the data given to the virtual-scroll, If I switch data using those tabs, the virtual scroll do update.

Expected behavior:

Virtual-scroll should refresh when items are added by the subscriber.

Steps to reproduce:

  1. In new application, create a virtual scroll in the home page binded to a list of items which is empty at first
  2. Asynchronously fill the data array without user interaction

Related code: Isssue is similar to #11532

Other information:

Ionic info:

Ionic:

   ionic (Ionic CLI)             : 4.10.2 (C:\Users\rmazu\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework               : @ionic/angular 4.1.1
   @angular-devkit/build-angular : 0.12.4
   @angular-devkit/schematics    : 7.3.5
   @angular/cli                  : 7.3.5
   @ionic/angular-toolkit        : 1.4.0

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 7.1.4
   Cordova Plugins       : cordova-plugin-ionic 5.3.0, cordova-plugin-ionic-keyboard 2.1.3, cordova-
plugin-ionic-webview 3.1.2, (and 7 other plugins)


System:

   Android SDK Tools : 26.1.1 (C:\DEV\AndroidSDK)
   NodeJS            : v10.15.0 (C:\DEV\nodejs\node.exe)
   npm               : 6.4.1
   OS                : Windows 10


Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
liamdebeasicommented, Apr 5, 2019

It depends on the Angular change detection strategy you’re using. By default they are different, but are similar when using the OnPush strategy in Angular.

1reaction
liamdebeasicommented, Mar 29, 2019

Hi there,

Thanks for the follow up. For performance reasons, Stencil (the web component compiler that Ionic uses) only compares references for running change detection, not the data inside of arrays or objects.

This means that the standard mutable array operations such as this.viewList.push(Math.random()); will not trigger change detection.

In your scenario, you will want to do something like:

  this.viewList = [
    ...this.viewList,
    Math.random()
  ]

The ... is the Spread operator which “expands” the given object in place: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Alternatively, you might want to consider doing this all at once after the loop has completed:

let tempArray  = [];
for (let i = 0; i < 30; i++) {
  tempArray.push(Math.random());
}

this.viewList = this.viewList.concat(tempArray);

You can learn more about how Stencil handles change detection here: https://stenciljs.com/docs/reactive-data

I am going to close this, but for further questions feel free to post on our forum or our slack.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Virtual-scroll not updating when initialized with empty data
With virtual-scroll, it works if the data array is given already with some element, but not if it is empty. I use tabs...
Read more >
Angular: cdkVirtualFor not rendering new items - Stack Overflow
The *cdkVirtualFor would only be updated if you update it immutably i.e. you cannot update the array after it is initialized.
Read more >
virtualScroll and empty list problem - ionic-v3
When using a virtualScroll list I'm having issues when the filter makes the list empty. In that case the view won't update.
Read more >
Lazy loading images and managing lists with Ionic VirtualScroll
Prevents jank while scrolling; Leverages in-memory caching. As the application that we'll develop will be working with a large list of data ( ......
Read more >
Large Trees & Virtual Scroll - Getting Started
If the tree is hidden (for example inside tab or modal), it will not be rendered when it becomes visible. ... Initializing 100,000...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found