Skip to content

Comments

fix(virtual-core): guard against null targetWindow in scrollToIndex rAF callback#1129

Open
ansmonjol wants to merge 1 commit intoTanStack:mainfrom
ansmonjol:fix/scrollToIndex-null-targetWindow
Open

fix(virtual-core): guard against null targetWindow in scrollToIndex rAF callback#1129
ansmonjol wants to merge 1 commit intoTanStack:mainfrom
ansmonjol:fix/scrollToIndex-null-targetWindow

Conversation

@ansmonjol
Copy link

@ansmonjol ansmonjol commented Feb 20, 2026

🎯 Changes

When a component unmounts while scrollToIndex is in progress, the cleanup method sets targetWindow to null. However, the already-scheduled requestAnimationFrame callback inside tryScroll still fires and accesses this.targetWindow without a null check, causing:

Cannot read properties of null (reading 'requestAnimationFrame')

The scheduleRetry function already guards against this (if (!this.targetWindow) return), but the rAF callback scheduled in tryScroll does not — and it uses a non-null assertion (this.targetWindow!) to schedule a second frame in dynamic mode.

Fix: Add an early return at the top of the rAF callback to bail out if targetWindow has been cleaned up between frames.

Test: Added a unit test that reproduces the crash by triggering scrollToIndex in dynamic mode, then simulating unmount before flushing the rAF callbacks. Confirms the test fails without the fix and passes with it.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot
Copy link

changeset-bot bot commented Feb 20, 2026

🦋 Changeset detected

Latest commit: ff6be66

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@tanstack/virtual-core Patch
@tanstack/angular-virtual Patch
@tanstack/lit-virtual Patch
@tanstack/react-virtual Patch
@tanstack/solid-virtual Patch
@tanstack/svelte-virtual Patch
@tanstack/vue-virtual Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ansmonjol ansmonjol marked this pull request as draft February 20, 2026 12:42
@ansmonjol ansmonjol force-pushed the fix/scrollToIndex-null-targetWindow branch from 8271ba8 to db3c5ad Compare February 20, 2026 12:52
@ansmonjol ansmonjol marked this pull request as ready for review February 20, 2026 12:53
…AF callback

When a component unmounts while scrollToIndex is in progress, the
cleanup method sets targetWindow to null. However, the already-scheduled
requestAnimationFrame callback in tryScroll still fires and accesses
this.targetWindow without a null check, causing "Cannot read properties
of null (reading 'requestAnimationFrame')".

Add an early return guard at the top of the rAF callback to bail out
if targetWindow has been cleaned up.
@ansmonjol ansmonjol force-pushed the fix/scrollToIndex-null-targetWindow branch from db3c5ad to ff6be66 Compare February 20, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants