fix(react-virtual): skip flushSync while measuring from the ref callback #1282
main fromrefs/pull/1282/head·merged by piecyk 🎯 Changes#
Fixes #1281.
Problem#
measureElement is handed to item elements as a ref, so React calls it during the commit phase. When the measured size differs from estimateSize for an item above the current scroll offset, resizeItem compensates the scroll position and raises a sync notify — and this adapter then calls flushSync(rerender) while React is still committing. React runs the callback but cannot flush, so development builds log
flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. …
once per measured item (21 on first mount and 11 per list switch in the app measured below). Previously reported in #628 and #1094.
Fix#
Mark the window in which the virtualizer measures from its ref callback, and skip flushSync only for notifies raised inside it:
- wrap
instance.measureElementonce, where the instance is created, to set and clearmeasuringFromRef onChangekeepsflushSyncfor every other sync notify —ResizeObserverre-measures and scroll adjustments arrive outside the commit phase and still flush synchronously
Why this keeps behaviour identical. React's flushSync sets Internals.p = 2 (DiscreteEventPriority) and runs the callback; while committing it only skips the flush (Internals.d.f() → flushSyncWork() returns true, which is what produces the warning). The commit phase itself already runs at DiscreteEventPriority, so an update scheduled from there lands in the same lane and is flushed at the same point with or without flushSync.
useFlushSync: false, the workaround suggested on the earlier issues, is broader: it also drops the ResizeObserver path to default priority, so rows that grow after mount (late-loading images) settle a frame later.
Test verification (RED -> GREEN)#
New test in packages/react-virtual/tests/index.test.tsx. The existing List helper gained an initialOffset prop so that measured items sit above the fold:
render(<List itemSize={100} dynamic initialOffset={400} />)
- RED — with the source change reverted, the test fails:
console.errorreceives theflushSyncwarning 3 times. - GREEN — with the fix,
packages/react-virtualpasses 8/8 (7 existing + the new one).
Browser verification in a real app#
React 19.2.8, @tanstack/react-virtual 3.14.12 + virtual-core 3.17.10, headless Chromium. One virtualized chat timeline mounted in isolation (80 rows, anchorTo: 'end', estimateSize 56 vs real 90–300), three phases: first mount → switch to another list → switch back. Three runs of each build; runs of the same build were frame-identical, so the comparison is not noise.
| before | after | |
|---|---|---|
flushSync warnings | 21 / 11 / 11 | 0 / 0 / 0 |
commits containing the list (React.Profiler) | 7 / 7 / 6 | 7 / 7 / 6 |
per-frame scrollTop + row transform, 40 frames per phase | — | identical, 120 / 120 frames |
The same change (applied to the published build with pnpm patch) is running in our application.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally — see below.
Local run on Node 24.8.0 (.nvmrc) with pnpm 11.9.0 (packageManager):
NX_BASE=<TanStack/virtual main> NX_HEAD=HEAD CI=1 pnpm run test:pr --parallel=2 --skip-nx-cache— every target passed except@tanstack/react-virtual:test:e2e, which could not launch a browser on this machine (Executable doesn't exist … chromium_headless_shell-1194).After
pnpm exec playwright install chromium,nx run @tanstack/react-virtual:test:e2e --skip-nx-cachepasses: 36 passed.nx run-many --targets=test:eslint,test:lib,test:types,test:build,build --parallel=2 --skip-nx-cache— 68 projects, all successful.Prettier passes for the changed TypeScript files and the changeset.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset (one patch for
@tanstack/react-virtual). - This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Prevented development warnings when virtualized list items are measured during rendering.
- Improved dynamic item measurement for lists with an initial scroll position.
- Preserved synchronous updates for other measurement and scrolling interactions.
- 1
View your CI Pipeline Execution ↗ for commit 982c579ae521e735bd2a4795797419abc53266c3
Command Status Duration Result nx run-many --target=build --exclude=examples/**✅ Succeeded 17s View ↗ nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 50s View ↗
☁️ Nx Cloud last updated this comment at
2026-09-14 09:19:29UTCMore templates
- @tanstack/virtual-example-angular-chat
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-marko-virtual-example-chat
- tanstack-marko-virtual-example-chat-pretext
- tanstack-marko-virtual-example-dynamic
- tanstack-marko-virtual-example-fixed
- tanstack-marko-virtual-example-grid
- tanstack-marko-virtual-example-infinite-scroll
- tanstack-marko-virtual-example-padding
- tanstack-marko-virtual-example-pretext
- tanstack-marko-virtual-example-scroll-padding
- tanstack-marko-virtual-example-smooth-scroll
- tanstack-marko-virtual-example-ssr
- tanstack-marko-virtual-example-ssr-fetch
- tanstack-marko-virtual-example-ssr-restore
- tanstack-marko-virtual-example-ssr-slice
- tanstack-marko-virtual-example-sticky
- tanstack-marko-virtual-example-table
- tanstack-marko-virtual-example-variable
- tanstack-marko-virtual-example-window
- tanstack-marko-virtual-example-window-ssr-slice
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
- tanstack-react-virtual-example-chat
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-pretext
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-window
@tanstack/angular-virtual
npm i https://pkg.pr.new/@tanstack/angular-virtual@1282@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1282@tanstack/marko-virtual
npm i https://pkg.pr.new/@tanstack/marko-virtual@1282@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1282@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1282@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1282@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1282@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1282commit:
982c579piecyk merged thisb48e3b0
piecyk closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
8e3f08ba-3f77-4594-a0fc-2488031f4c72📥 Commits
Reviewing files that changed from the base of the PR and between 2c0a0ea101c360ea15832c060008aa163cae2f7c and 1d8a1c6de39e6c36fe81d5da8b9ffb02228a25f1.
📒 Files selected for processing (3)
.changeset/sweet-pugs-marry.mdpackages/react-virtual/src/index.tsxpackages/react-virtual/tests/index.test.tsxIncluded review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough#
The React adapter now skips
flushSynconly whilemeasureElementruns from a ref callback during commit. Tests cover dynamic measurement with an initial offset, and a changeset records a patch release.Changes#
React virtualizer flush handling
packages/react-virtual/src/index.tsx,.changeset/sweet-pugs-marry.mdflushSyncfor synchronous notifications raised during that window. Other synchronous notification paths still useflushSync.packages/react-virtual/tests/index.test.tsxinitialOffset, and a regression test verifies that dynamic ref-based measurement does not log aflushSyncwarning.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~15 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
piecykMerge Risk: ⚪ Minimal · up to
982c5The commit-phase warning is addressed without an established regression to other measurement or scrolling behavior.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1281.packages/react-virtual/src/index.tsxtracks themeasureElementref-callback window and callsrerender()withoutflushSynconly for synchronous notifications i…#1281. TheinitialOffsettest-helper support enables the required reproduction. The regression test verifies warning removal. The changeset records the patch release fo…Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share