fix(svelte): filter stale indices in Virtualizer to prevent undefined data access#847
Merged
inokawa merged 3 commits intoinokawa:mainfrom Feb 16, 2026
Merged
Conversation
… data access Add data.length dependency to indexes derived and filter indices to valid range. This prevents stale indices from causing "Cannot read properties of undefined" errors during reactive updates when data array length changes (e.g., delete operations). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Owner
|
Hi, could you provide a reproducible code for the problem? |
Contributor
Author
|
Here's a minimal reproduction you can paste into the Svelte playground: <script>
import { VList } from "virtua/svelte";
let items = $state(
Array.from({ length: 100 }, (_, i) => ({ id: i, text: `Item ${i}` }))
);
function deleteItem(id) {
items = items.filter((item) => item.id !== id);
}
</script>
<!--
Steps to reproduce:
1. Scroll down to the bottom of the list (items 95-99)
2. Click "Delete" on any visible item
3. BUG: "Cannot read properties of undefined" error
The range still contains indices 95-99, but after deletion
data.length is 99, so data[99] is undefined.
-->
<div style="height: 400px; overflow: auto;">
<VList data={items} getKey={(item) => item.id}>
{#snippet children(item, index)}
<div style="height: 50px; padding: 8px; border-bottom: 1px solid #ccc; display: flex; justify-content: space-between; align-items: center;">
<span>{item.text}</span>
<button onclick={() => deleteItem(item.id)}>Delete</button>
</div>
{/snippet}
</VList>
</div>Steps to trigger the bug:
|
inokawa
approved these changes
Feb 16, 2026
Owner
|
Shipped in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #851
Summary
data.lengthdependency toindexesderived to ensure recalculation when data changesindex >= 0 && index < len) to prevent stale indicesProblem
During reactive updates (e.g., delete operations), the
indexesderived could contain stale indices that exceed the currentdata.length, causing "Cannotread properties of undefined" errors when accessing
data[index]in the#eachblock.Solution
By accessing
data.lengthin theindexesderived, we create a reactive dependency that ensuresindexesrecalculates atomically with data changes. Thefiltering ensures only valid indices are included.
Test plan