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
Describe the bug
getItemKeymight be called with an index which is out of range of the currentcount. It can happen becauseResizeObservermight be called with the previous nodes and new nodes after the list is changed. For example,Previously, we were checking if there is an item with such index in
measurementsCachebut now we don't check it and because of thatgetItemKeyis 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 callgetItemKeywith the previous indexes.I can accomplish this partially by falling back to an index in
getItemKey, e.g.:In this case it will unobserver the previous nodes. But I'm not sure if it's intended way of using
getItemKeyand it doesn't clearmeasureElementCache(this.measureElementCache.delete(elementKey)).tanstack-virtual version
v3.0.0-beta.59
Terms & Code of Conduct