Skip to content
Source
Sign inSign up
TanStack/virtual

feat: add `deferFlushSync` option for React 19 compatibility #1098

Closedchannyeintun(GitHub profile, opens in a new tab)wants to merge 6 commits intomain fromrefs/pull/1098/headopened

React 19 throws a warning when flushSync is called from inside a lifecycle method (useLayoutEffect). This change adds a new deferFlushSync option that users can enable to defer flushSync to a microtask, suppressing the warning.

Fixes #1094

🎯 Changes

Problem

When using @tanstack/react-virtual with React 19, users see the following warning in the console:

flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.

This occurs because flushSync(rerender) is called inside the onChange callback, which can be triggered during useLayoutEffect via instance._willUpdate().

Solution

Added a new deferFlushSync option that users can enable to defer the flushSync call:

const virtualizer = useVirtualizer({
  count: items.length,
  getScrollElement: () => parentRef.current,
  estimateSize: () => 50,
  deferFlushSync: true, // Opt-in to suppress React 19 warning
})

When enabled, the flushSync call is wrapped in queueMicrotask():

  if (sync) {
-   flushSync(rerender)
+   if (options.deferFlushSync) {
+     queueMicrotask(() => flushSync(rerender))
+   } else {
+     flushSync(rerender)
+   }
  }

Why opt-in instead of default?

  • The synchronous flushSync behavior is intentional for scroll correction during measurement
  • Deferring to a microtask may cause visible white space gaps during fast scrolling with dynamic measurements
  • Users experiencing the React 19 warning can choose to accept this tradeoff

Files Changed

  • packages/virtual-core/src/index.ts - Add deferFlushSync option to VirtualizerOptions
  • packages/react-virtual/src/index.tsx - Conditionally wrap flushSync based on option
  • packages/react-virtual/tests/index.test.tsx - Add test for deferFlushSync behavior

✅ 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).
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: fa4acc3649420f84bdc9c9b54c6a9a6c2a30c11c

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

      This PR includes changesets to release 7 packages
      NameType
      @tanstack/react-virtualPatch
      @tanstack/virtual-corePatch
      @tanstack/angular-virtualPatch
      @tanstack/lit-virtualPatch
      @tanstack/solid-virtualPatch
      @tanstack/svelte-virtualPatch
      @tanstack/vue-virtualPatch

      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

    2. @piecyk Hi, could you review this PR please?

    3. @channyeintun Thanks for taking a look at this.

      Unfortunately, deferring flushSync via queueMicrotask() fundamentally changes the behavior here.

      In this code path, flushSync is intentionally used to synchronously apply the scroll correction during measurement to limit visible white space while scrolling. By pushing it into a microtask, React has already completed the render/commit phase and the scroll has progressed further, effectively making this flushSync a no-op for its original purpose.

      In practice this reintroduces the exact issue flushSync was added to solve: noticeable gaps during fast scrolling with dynamic measurements.

      Instead of changing the default behavior, I think we should make this configurable under a flag in @tanstack/react-virtual.

      ps: the warning itself doesn’t really affect anything in this case.

    4. @channyeintun Thanks for taking a look at this.

      Unfortunately, deferring flushSync via queueMicrotask() fundamentally changes the behavior here.

      In this code path, flushSync is intentionally used to synchronously apply the scroll correction during measurement to limit visible white space while scrolling. By pushing it into a microtask, React has already completed the render/commit phase and the scroll has progressed further, effectively making this flushSync a no-op for its original purpose.

      In practice this reintroduces the exact issue flushSync was added to solve: noticeable gaps during fast scrolling with dynamic measurements.

      Instead of changing the default behavior, I think we should make this configurable under a flag in @tanstack/react-virtual.

      ps: the warning itself doesn’t really affect anything in this case.

      Good call - I didn't consider that deferring breaks the scroll correction. I've changed it to an opt-in flag (deferFlushSync) so the default stays synchronous. Users who hit the warning can enable it if they're okay with the tradeoff.

    5. piecyk requested changes

      Thanks for working on this 👍

      I’ve opened a follow-up PR https://github.com/TanStack/virtual/pull/1100 that moves this behind a React-specific option in @tanstack/react-virtual, rather than exposing it in virtual-core. Since this behavior is tied to flushSync and React lifecycle semantics, it felt better to keep core framework-agnostic and make the trade-off explicit at the React layer (with docs).

      Let’s continue the discussion there, once again thanks for this, and I’ve requested a review from you as well 🙂

      @@ -348,3 +348,13 @@
      348348 enabled?: boolean
      349349 isRtl?: boolean
      350350 useAnimationFrameWithResizeObserver?: boolean
      351+ /**
      352+ * If true, defers the flushSync call during synchronous updates to a microtask.
      353+ * This suppresses the React 19 warning: "flushSync was called from inside a lifecycle method."
      354+ *
      355+ * @warning Enabling this may cause visible white space gaps during fast scrolling
      356+ * with dynamic measurements, as the scroll correction won't be applied synchronously.
      357+ *
      358+ * @default false
      359+ */
      360+ deferFlushSync?: boolean
      piecykcommented

      I think this be only react specific, also let's use useFlushSync

      @@ -28,4 +28,5 @@
      2828 ...options,
      2929 onChange: (instance, sync) => {
      3030 if (sync) {
      31- flushSync(rerender)
      31+ if (options.deferFlushSync) {
      32+ queueMicrotask(() => flushSync(rerender))
      piecykcommented

      Drop the queueMicrotask

      @@ -28,4 +28,4 @@
      2828 ...options,
      2929 onChange: (instance, sync) => {
      3030 if (sync) {
      31- flushSync(rerender)
      31+ if (options.deferFlushSync) {
      piecykcommented

      Lift the check to sync

    6. @piecyk Thanks for the follow-up and for opening the PR!

      I agree with the approach of keeping this out of virtual-core. Since it’s tightly coupled to React’s lifecycle and flushSync, putting it behind a React-specific option feels like the right call and keeps the core clean. Making the trade-off explicit in the React layer (and docs) seems reasonable.

      I’ll jump into #1100 and we can continue the discussion there. Thanks again for pushing this and for the review request 🙂

    7. Since #1100 is the solution we need, let me close this PR. Thank you.

Sign in to join this conversation.