Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
Dugnist opened this issue Apr 4, 2024 · 7 comments
Open
2 tasks done

Comments

@Dugnist
Copy link

Dugnist commented Apr 4, 2024

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/[email protected]/+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

<script type="module" defer>
  import { Virtualizer } from 'https://cdn.jsdelivr.net/npm/@tanstack/[email protected]/+esm';
</script>
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

[email protected]

TypeScript version

vanilla js

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
@andrody
Copy link

andrody commented May 10, 2024

same problem on safari

@piecyk
Copy link
Collaborator

piecyk commented May 14, 2024

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

@vincehi
Copy link

vincehi commented Jun 11, 2024

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
Copy link

lucjdo commented Jun 19, 2024

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

@piecyk
Copy link
Collaborator

piecyk commented Jun 19, 2024

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

@lucjdo can you create simple stackblitz example?

@lucjdo
Copy link

lucjdo commented Jun 19, 2024

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

@kapilpipaliya
Copy link

I used setTimeout(() => setParentRef(el), 100) to set parentRef to solve the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants