TanStack / virtual

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
https://tanstack.com/virtual
MIT License
5.25k stars 271 forks source link

virtualizer.getVirtualItems() return zero items for vanilla js project #699

Open Dugnist opened 3 months ago

Dugnist commented 3 months ago

Describe the bug

I need to implement virtual list in vanilla js. I used this approach:

<script type="module" defer>
  import { Virtualizer } from 'https://cdn.jsdelivr.net/npm/@tanstack/virtual-core@3.2.0/+esm';
  // other code...
  // Initialize the virtualizer
  const virtualizer = new Virtualizer({
    debug: true,
    count: 10000, // The number of items in the list
    getScrollElement: () => container,
    estimateSize: () => 50, // The estimated size of each item
    overscan: 5, // How many items to render outside of the viewport
  });
  // other code...
  const items = virtualizer.getVirtualItems();
  // items.length always === 0
</script>

No matter what I do, the array of items is always empty. But virtualizer.measurementsCache.length === 10000.

Your minimal, reproducible example

https://codepen.io/Dugnist/pen/LYveoXQ

Steps to reproduce

1)

<script type="module" defer>
  import { Virtualizer } from 'https://cdn.jsdelivr.net/npm/@tanstack/virtual-core@3.2.0/+esm';
</script>

2)

const virtualizer = new Virtualizer({
    debug: true,
    count: 10000, // The number of items in the list
    getScrollElement: () => container,
    estimateSize: () => 50, // The estimated size of each item
    overscan: 5, // How many items to render outside of the viewport
  });

3

const items = virtualizer.getVirtualItems(); // -> []

Expected behavior

const items = virtualizer.getVirtualItems() should return array with more than zero items

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

codepen

tanstack-virtual version

virtual-core@3.2.0

TypeScript version

vanilla js

Additional context

No response

Terms & Code of Conduct

andrody commented 2 months ago

same problem on safari

piecyk commented 2 months ago

@Dugnist you need to call virtualizer._willUpdate() that will register scroll events and rect size detection.

vincehi commented 1 month ago

I'm using solid, I had the same problem when I tried to update the package, so I went back to the one I was using until then, 3.0.0-beta.6, and it works perfectly. Waiting to find out why.

lucjdo commented 4 weeks ago

having same problem on a nextJs project with latest version (3.5.1)

piecyk commented 3 weeks ago

having same problem on a nextJs project with latest version (3.5.1)

@lucjdo can you create simple stackblitz example?

lucjdo commented 3 weeks ago

having same problem on a nextJs project with latest version (3.5.1)

@lucjdo can you create simple stackblitz example?

@piecyk sorry my bad, I was trying to use it with another library and the parent ref wasn't accurate, that was causing getVirtualItems() to return a empty array