Skip to content

Comments

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

Merged
piecyk merged 2 commits intoTanStack:mainfrom
ansmonjol:fix/scrollToIndex-null-targetWindow
Feb 23, 2026
Merged

fix(virtual-core): guard against null targetWindow in scrollToIndex rAF callback#1129
piecyk merged 2 commits intoTanStack:mainfrom
ansmonjol:fix/scrollToIndex-null-targetWindow

Conversation

@ansmonjol
Copy link
Contributor

@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: c17d70b

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
Copy link
Collaborator

@piecyk piecyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, let's remove the unsafe exclamation mark

@nx-cloud
Copy link

nx-cloud bot commented Feb 23, 2026

View your CI Pipeline Execution ↗ for commit c17d70b

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 56s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 15s View ↗

☁️ Nx Cloud last updated this comment at 2026-02-23 22:13:29 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 23, 2026

More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1129

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1129

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1129

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1129

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1129

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1129

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1129

commit: ff6be66

Co-authored-by: Damian Pieczynski <piecyk@gmail.com>
@ansmonjol
Copy link
Contributor Author

Make sense, change is applied @piecyk

@ansmonjol ansmonjol requested a review from piecyk February 23, 2026 18:35
@piecyk piecyk merged commit 843109c into TanStack:main Feb 23, 2026
@github-actions github-actions bot mentioned this pull request Feb 23, 2026
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.

3 participants