Skip to content

getItemKey is called with invalid index #580

Description

@lesha1201

Describe the bug

getItemKey might be called with an index which is out of range of the current count . It can happen because ResizeObserver might be called with the previous nodes and new nodes after the list is changed. For example,

[
  <previous-node data-index="0" />,
  <previous-node data-index="1" />,
  <new-node data-index="0" />,
]

Previously, we were checking if there is an item with such index in measurementsCache but now we don't check it and because of that getItemKey is called with a wrong index.

https://github.com/TanStack/virtual/pull/521/files#diff-85d1b85f3d4e501779ca41e51cd38cf7f61a2b7ffddaf958cd7e79c2787c36f1L564-L569

Expected behavior

Not sure what's the right behavior. I guess we should call this.observer.unobserve(node) for the previous nodes here and at the same time to not call getItemKey with the previous indexes.

I can accomplish this partially by falling back to an index in getItemKey, e.g.:

const getItemKey = (index) => data[index]?.key || index

In this case it will unobserver the previous nodes. But I'm not sure if it's intended way of using getItemKey and it doesn't clear measureElementCache (this.measureElementCache.delete(elementKey)).

tanstack-virtual version

v3.0.0-beta.59

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions