TanStack / TanStack/virtual

virtualizer.getVirtualItems() mismatches with number provided in count attribute during initialisation

Open
#666 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
466
Avg merge
2d 31m
Merged PRs (30d)
13

Description

Describe the bug

On Re-Render the number returned by virtualizer.getVirtualItems() is different than the number provided during initialisation of the virtualiser.

Initialising Virtualiser
const count = props.attributeGroups.length;
  const parentRef = React.useRef<HTMLDivElement>(null);
  const virtualizer = useVirtualizer({
    count,
    getScrollElement: () => parentRef.current,
    estimateSize: index => {
      const group = props.attributeGroups[index];
      let size = VIRTUALISED_ATTRIBUTE_GROUP_DEFAULT_SIZE;
      if (group) {
        size =
          group.attributes.length * VIRTUALISED_ATTRIBUTE_ELEMENT_DEFAULT_SIZE;
      }
      return size;
    },
    onChange: props.handleNavBarChangeOnScroll
  });

const items = virtualizer.getVirtualItems();
const totalSize = virtualizer.getTotalSize();



return (
    <div ref={parentRef}>
      <div id={`${props.currentMenu.name}-attributeGroupsContainer`} style={{ height: `${totalSize}px` }}>
        {items.map(virtualItem => (
          <div
            key={virtualItem.key}
            data-index={virtualItem.index}
            ref={virtualizer.measureElement}
            style={{
              position: "relative",
              width: "100%",
            }}
          >
            <GroupContainer
              {...props}
              key={props.attributeGroups[virtualItem.index].name}
              group={props.attributeGroups[virtualItem.index]}
            />
          </div>
        ))}
      </div>
    </div>
  );

Obeserved : In the above code with console.log(), items.length and count are different (items.length is 8 whereas count is 21) for some reason when props change but the totalSize however is computed for 21 components (more than required) due to which there is a empty section on the page.

Expected: Both items.length and count should be the same. (Please correct me if I am wrong in assuming this)

Regards,
Abhishek

Your minimal, reproducible example

NA

Steps to reproduce

NA

Expected behavior

Obeserved : In the above code with console.log(), items.length and count are different (items.length is 8 whereas count is 21) for some reason when props change but the totalSize however is computed for 21 components (more than required) due to which there is a empty section on the page.

Expected: Both items.length and count should be the same. (Please correct me if I am wrong in assuming this)

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

Chrome, Mozilla

tanstack-virtual version

3.0.0-beta.54

TypeScript version

4.9.5

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the useVirtualizer configuration and trace how count, getVirtualItems(), and getTotalSize() respond when props change. A reproducible example is needed before comparing the reported item count with the expected virtualization behavior; done means the discrepancy is explained or corrected and the expected result is demonstrated.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.