From 7857d7a89f319d177061fe54fa4148cbcd85e7d2 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 20 May 2025 11:38:02 +0200 Subject: [PATCH 1/2] fix(virtual-core): silently fail instead of throwing an error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before the this code runs, we already verify that the offset for a specific index exists. If this is not the case, then we bail by just using a `return` However, the code changed in this commit then assumes that the same conditions hold true. However, this is also running in a `setTimeout` which means that values could have changed in the meantime (which can happen due to React re-renders). In our particular issue in Headless UI, it's because somebody is filtering a Combobox while typing. This in turn changes the `enabled` and `count` from the `useVirtualizer` options. How we use it in Headless UI: ``` let virtualizer = useVirtualizer({ enabled: options.length !== 0, count: options.length, // … }) ``` Another issue is that because of the `setTimeout`, you can't even catch this error in user land. So to fix it, once we check if the offset is still available inside of the `setTimeout`, we will also just silently bail if it results in `undefined`. --- packages/virtual-core/src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 3c704059c..fc6449839 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -998,9 +998,10 @@ export class Virtualizer< ) if (elementInDOM) { - const [latestOffset] = notUndefined( - this.getOffsetForIndex(index, align), - ) + const result = this.getOffsetForIndex(index, align) + if (!result) return + const [latestOffset] = result + const currentScrollOffset = this.getScrollOffset() if (!approxEqual(latestOffset, currentScrollOffset)) { this.scrollToIndex(index, { align, behavior }) From 257fa0db368808993a2d184e3d81431971bfc284 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 20 May 2025 11:58:27 +0200 Subject: [PATCH 2/2] add changeset --- .changeset/chatty-ravens-stay.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chatty-ravens-stay.md diff --git a/.changeset/chatty-ravens-stay.md b/.changeset/chatty-ravens-stay.md new file mode 100644 index 000000000..231f4c94b --- /dev/null +++ b/.changeset/chatty-ravens-stay.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': patch +--- + +fix(virtual-core): fix `Error: Unexpected undefined`