Skip to content

fix(react-virtualizer): prevent RangeError when numItems decreases while scrolled near bottom#616

Merged
dmytrokirpa merged 3 commits intomicrosoft:mainfrom
dmytrokirpa:fix/react-virtualizer-negative-array-length
Mar 3, 2026
Merged

fix(react-virtualizer): prevent RangeError when numItems decreases while scrolled near bottom#616
dmytrokirpa merged 3 commits intomicrosoft:mainfrom
dmytrokirpa:fix/react-virtualizer-negative-array-length

Conversation

@dmytrokirpa
Copy link
Contributor

@dmytrokirpa dmytrokirpa commented Mar 3, 2026

Summary

Fixes #607 (in microsoft/fluentui-contrib)

  • renderChildRows: wrap numItems - newIndex with Math.max(0, ...) to prevent a negative arrayLength from being passed to new Array(), which threw RangeError: Invalid array length when numItems decreased (e.g. filtering) while actualIndex was still at a high scroll position.
  • calculateBefore: wrap Math.min(actualIndex, numItems - 1) with Math.max(0, ...) to prevent a negative beforeBufferHeight for static-sized lists when numItems drops to 0.

Both fixes mirror the guard already present in updateChildArray (Math.max(0, numItems - startIndex)).

Test plan

  • New Jest regression test added: "should not crash with RangeError when numItems decreases while scrolled near the bottom" — simulates contextIndex = 90 with numItems filtered from 100 → 10, asserts no RangeError is thrown and virtualizedChildren.length >= 0.
  • All existing useVirtualizer unit tests pass (yarn jest useVirtualizer.test).

🤖 Generated with Claude Code

…ile scrolled near bottom

When filtering reduces numItems while actualIndex still holds a high
scroll position, renderChildRows computed a negative arrayLength causing
"RangeError: Invalid array length". Clamp arrayLength to zero with
Math.max(0, ...) mirroring the guard already present in updateChildArray.

Also guard calculateBefore against returning a negative buffer height
when numItems is 0 (static sizing path).

Fixes microsoft#607

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dmytrokirpa dmytrokirpa self-assigned this Mar 3, 2026
@dmytrokirpa dmytrokirpa added bug Something isn't working react-virtualizer labels Mar 3, 2026
@dmytrokirpa dmytrokirpa force-pushed the fix/react-virtualizer-negative-array-length branch from 93cb439 to 13abf38 Compare March 3, 2026 12:33
@dmytrokirpa dmytrokirpa marked this pull request as ready for review March 3, 2026 12:33
@dmytrokirpa dmytrokirpa requested review from a team as code owners March 3, 2026 12:33
@dmytrokirpa dmytrokirpa enabled auto-merge (squash) March 3, 2026 12:33
@dmytrokirpa dmytrokirpa merged commit ba987ed into microsoft:main Mar 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working react-virtualizer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug](react-virtualizer): Virtualizer crashes when the number of items changes to be smaller than the current scroll position

2 participants