Skip to content

Virtualizer's CollectionRendererContext leaks into overlay content, crashing nested collections #10500

Closed

Description

@coolassassin

Provide a general summary of the issue here

When a collection is virtualized with <Virtualizer> (e.g. a virtualized Table), the Virtualizer publishes a CollectionRendererContext ({ isVirtualized: true, CollectionRoot, layout, dropTargetDelegate }) that wraps its entire React subtree. Any other collection component (Menu, ListBox, Select, …) rendered inside an overlay (Popover / Dialog) that is a React descendant of the virtualized collection inherits that context — React context propagates through portals — and renders its own collection with the ancestor's CollectionRoot / layout.

If the ancestor layout is a TableLayout (virtualized Table), the nested overlay collection (a Menu/ListBox collection, which has no columns) is laid out with TableLayout, and TableLayout.buildCollection() throws:

TypeError: collection.columns is not iterable

In practice: a virtualized Table whose column headers contain menus/selects/filter popovers crashes the whole table (via the error boundary) the moment any of those popovers is opened.

🤔 Expected Behavior?

Overlay content (Popover / Dialog / Modal) should not inherit a virtualized ancestor's CollectionRendererContext. A Menu / ListBox / Select opened from inside a virtualized Table (or any virtualized collection) should render its own collection with the default renderer (or with its own Virtualizer if one is explicitly nested), not with the ancestor collection's layout. Opening the popover should just work.

😯 Current Behavior

Opening the overlay throws and unmounts the tree:

TypeError: collection.columns is not iterable
    at TableLayout.buildCollection (TableLayout.ts:169:35)
    at TableLayout.update (ListLayout.ts:359:27)   // super.update
    at TableLayout.update (TableLayout.ts:162:11)
    at Virtualizer.relayout (Virtualizer.ts:194:17)
    at Virtualizer.render (Virtualizer.ts:419:12)
    at useVirtualizerState (useVirtualizerState.ts:91:34)
    at CollectionRoot (Virtualizer.tsx:108:15)

Root cause chain:

  1. Virtualizer (Virtualizer.tsx) provides CollectionRendererContext with { isVirtualized: true, CollectionRoot, layout, dropTargetDelegate } around its children.
  2. Menu (Menu.tsx, let { isVirtualized, CollectionRoot } = useContext(CollectionRendererContext)) and ListBox (ListBox.tsx, reads isVirtualized / CollectionRoot / layoutDelegate from the same context) render their collection through the inherited CollectionRoot. Neither resets the context, and Popover/Dialog do not reset it either.
  3. Because the popover content is still a React descendant of the Virtualizer (portals preserve React context), the menu/listbox is laid out with the ancestor's TableLayout.
  4. TableLayout.buildCollection() does for (let column of collection.columns), but the overlay's collection is a Menu/ListBox collection (not a TableCollection), so collection.columns is undefined → not iterable.

Note: a real (even empty) TableCollection always exposes columns (columns: GridNode<T>[] = []), so this only happens because a non-table collection reaches the table layout.

💁 Possible Solution

The overlay boundary should reset the collection renderer so nested collections don't inherit an unrelated virtualized ancestor's renderer. Options:

  • Have Popover / Dialog / Modal (or the overlay Provider inside them) reset CollectionRendererContext to DefaultCollectionRenderer for their children, or
  • Have MenuTrigger / Select / ComboBox reset it around the Popover + ListBox/Menu they render.

Consumer-side workaround (works today, since both symbols are publicly exported from react-aria-components):

import { CollectionRendererContext, DefaultCollectionRenderer } from 'react-aria-components'

<Popover>
  <CollectionRendererContext.Provider value={DefaultCollectionRenderer}>
    {/* Menu / ListBox / Select content */}
  </CollectionRendererContext.Provider>
</Popover>

This is a no-op outside a virtualizer (the context is already DefaultCollectionRenderer), and a nested explicit <Virtualizer> inside the popover still overrides it, so no legitimate case is broken.

🔦 Context

We virtualize a data grid built on Table using Virtualizer + a custom TableLayout subclass. Column header cells contain menus (sort/reorder/resize actions), a column-select menu, and filter popovers. After migrating the grid to Virtualizer, opening any header popover crashes the entire grid through the error boundary — it makes header menus/selects/filters unusable in a virtualized table. This is a trading application where the grid is central, so the whole feature is blocked without the workaround above.

🖥️ Steps to Reproduce

Minimal reproduction (a virtualized Table whose column header contains a MenuTrigger → Popover → Menu):

import {
  Virtualizer,
  TableLayout,
  Table,
  TableHeader,
  TableBody,
  Column,
  Row,
  Cell,
  MenuTrigger,
  Button,
  Popover,
  Menu,
  MenuItem,
} from 'react-aria-components'

function Example() {
  return (
    <div style={{ height: 200 }}>
      <Virtualizer layout={TableLayout} layoutOptions={{ rowHeight: 32 }}>
        <Table aria-label="Example">
          <TableHeader>
            <Column isRowHeader>
              Name
              <MenuTrigger>
                <Button aria-label="Menu">☰</Button>
                <Popover>
                  <Menu>
                    <MenuItem>Sort ascending</MenuItem>
                    <MenuItem>Sort descending</MenuItem>
                  </Menu>
                </Popover>
              </MenuTrigger>
            </Column>
          </TableHeader>
          <TableBody>
            <Row>
              <Cell>Alpha</Cell>
            </Row>
            <Row>
              <Cell>Bravo</Cell>
            </Row>
          </TableBody>
        </Table>
      </Virtualizer>
    </div>
  )
}

Steps:

  1. Render the virtualized table above — it renders fine.
  2. Click the ☰ menu button in the column header to open the Popover + Menu.
  3. The app throws TypeError: collection.columns is not iterable and the tree unmounts (error boundary).

The same crash occurs with Select/ComboBox/ListBox inside a Popover in the header, and generally with any Menu/ListBox/Select rendered in an overlay under a virtualized Table.

(A live StackBlitz/CodeSandbox based on the snippet above can be added.)

Version

1.20.0

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

MacOS

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Activity

  1. LFDanLu commented on Aug 26, 2026

    @LFDanLu
    Member

    The above reproduction code seems to work in https://stackblitz.com/edit/5mdlxu8i?file=src%2FExample.tsx, do you have a repo or something you can share?

  2. LFDanLu commented on Aug 27, 2026

    @LFDanLu
    Member

    thanks for the repo, is there a particular use case as to why the MenuTrigger is being rendered inside the Virtualizer as a sibling of the Table? I saw the comment saying:

    That's the shape of a toolbar button next to a virtualized table (customize columns, global filter, bulk row actions, etc.) - a very ordinary pattern.
    

    but can't you just move it outside the Virtualizer since the Virtualizer is meant to apply specifically to a single wrapped collection component? We could certainly clear the context defensively but the Virtualizer was always meant to specifically closely wrap a single collection component IIRC

  3. coolassassin commented on Aug 27, 2026

    @coolassassin
    Author

    @LFDanLu Sorry for the noise — my original repro was slightly mis-targeted. Wrapping MenuTrigger directly inside <Column> doesn't reproduce because <Column> and <Cell> already reset CollectionRendererContext back to DefaultCollectionRenderer around their own children (that's been there since #6640, it's not a recent fix). That's why your Stackblitz worked fine.

    The leak is still there though — it just isn't gated on <Column>/<Cell> at all. Any overlay-triggering element that's a React descendant of <Virtualizer> but not wrapped by <Column>/<Cell> still inherits the ancestor's isVirtualized/CollectionRoot/layout. A toolbar button placed next to the table (customize columns, global filter, bulk row actions — all pretty ordinary patterns) is enough:

    <Virtualizer layout={TableLayout} layoutOptions={{ rowHeight: 32 }}>
      <div>
        {/* sibling of <Table>, still inside <Virtualizer> - not wrapped by Column/Cell */}
        <MenuTrigger>
          <Button aria-label="Menu">☰ Table menu</Button>
          <Popover>
            <Menu>
              <MenuItem>Sort ascending</MenuItem>
              <MenuItem>Sort descending</MenuItem>
            </Menu>
          </Popover>
        </MenuTrigger>
        <Table aria-label="Example">
          <TableHeader>
            <Column isRowHeader>Name</Column>
          </TableHeader>
          <TableBody>
            <Row><Cell>Alpha</Cell></Row>
            <Row><Cell>Bravo</Cell></Row>
          </TableBody>
        </Table>
      </div>
    </Virtualizer>

    Clicking the menu button crashes the whole tree, verified against react-aria-components@1.20.0 in a real browser:

    TypeError: Cannot read properties of undefined (reading 'length')
        at TableLayout.columnsChanged
        at TableLayout.update
        at Virtualizer.relayout
    

    (It reproduces even more minimally without <Table> at all — just <Virtualizer layout={TableLayout}><MenuTrigger>…</MenuTrigger></Virtualizer>.)

    Which brings me to the actual point I want to raise: I don't think resetting the context at <Column>/<Cell> is the right place to fix this, even as a stopgap. It only protects consumers who go through RAC's own <Column>/<Cell> components. Anyone building a custom grid/table on top of the lower-level react-aria hooks (useGridCell, useTableCell, etc. — which is exactly what we do for keyboard-navigation/focus reasons) reimplements the column/cell wrapper themselves and never gets that reset, so the leak is still fully live for us regardless of RAC version. Effectively the fix is invisible unless you happen to render your table cells through RAC's own components verbatim.

    It seems like the more robust place to reset CollectionRendererContext is wherever a new collection actually gets created — i.e. inside the components that call useContext(CollectionRendererContext) to render themselves as a root collection: Menu, ListBox, GridList, Tree, Select, ComboBox/Autocomplete, TagGroup, Tabs, etc. Since each of these is itself a distinct collection consumer, they're the ones that should decide whether they're virtualized (only if there's a <Virtualizer> that's actually theirs, not an ancestor's), rather than relying on some outer container (Column/Cell/whatever a consumer's own DOM/JSX structure happens to be) to have pre-reset the context for them. That would fix the leak structurally for any consumer — RAC's own <Table>, our custom wrapper, or anyone else's — instead of patching it ad hoc at every place a collection might be rendered inside a virtualized ancestor.

    There's arguably an even more general boundary worth considering: Popover (and overlay/portal machinery in general - Dialog, Modal, Tooltip) could reset CollectionRendererContext back to DefaultCollectionRenderer by default for their content. An overlay is conceptually detached from its DOM ancestor even though React context still flows through the portal - a virtualized layout from the trigger's ancestor tree essentially never makes sense for content rendered inside a popover/dialog/modal. Resetting there would close off this whole class of bugs by default, independent of whichever collection component happens to be rendered inside (including ones RAC doesn't know about, or nested overlays). It could complement (not replace) resetting at the collection-creating components mentioned above - the two together would make this basically impossible to hit by accident.

    Happy to help test if either direction is worth pursuing.

  4. LFDanLu commented on Aug 27, 2026

    @LFDanLu
    Member

    I see, I think the latter suggestion of clearing at overlay boundary makes sense, would you like to prototype that? That being said, just to clarify your own use case: are you using RAC components alongside your own react-aria hook built components? I assume so since you are using the Virtualizer but using the hooks to build custom grids/tables?

  5. snowystinger commented on Aug 27, 2026

    @snowystinger
    Member

    If we can avoid pulling Contexts into unrelated components we should, it makes initial bundle size larger and can increase overall bundle size for others

  6. su-jin1425 commented on Aug 28, 2026

    @su-jin1425
    Contributor

    Hi, I’m interested in working on this issue. I saw the discussion about prototyping the overlay-boundary approach, so I wanted to check whether anyone is currently working on it or planning to open a PR. If it’s available, I’d be happy to investigate it.

  7. coolassassin commented on Aug 31, 2026

    @coolassassin
    Author

    @LFDanLu

    I see, I think the latter suggestion of clearing at overlay boundary makes sense, would you like to prototype that? That being said, just to clarify your own use case: are you using RAC components alongside your own react-aria hook built components? I assume so since you are using the Virtualizer but using the hooks to build custom grids/tables?

    Unfortunately, I don't have resources to make a prototype in short term perspective. Maybe later

    Answering to your second question, yes, we use combination of RAC component and our own components, because we need a bit more flexibility (#10121) and performance in some cases (#10088, #10477, #10490)
    But, we would like to use out of box solution

  8. LFDanLu commented on Aug 31, 2026

    @LFDanLu
    Member

    @su-jin1425 feel free to pick this up!

  9. su-jin1425 commented on Aug 31, 2026

    @su-jin1425
    Contributor

    Great i will work on it.

  10. su-jin1425 commented on Sep 1, 2026

    @su-jin1425
    Contributor

    Hi @LFDanLu I’ve completed the fix for #10500 and pushed the final changes to PR #10544. The overlay-boundary approach is implemented, the regression tests are passing, and all CI checks are now green. The final diff is limited to the necessary changes.

  11. added 3 commits that reference this issue on Sep 2, 2026
    8bf5bff
    55ec546
    2e3256d
  12. snowystinger commented on Sep 3, 2026

    @snowystinger
    Member

    Thanks for the Issue, after explorations in @su-jin1425 's PR this is the expected behaviour. We cannot know what collection a particular virtualizer context is meant for. We actually rely on the context going through overlays right now, which means others may be relying on this as well if they copied our work as a starting point.

    What you could do though is wrap the Virtualizer and create your own forwarding context and immediately clear the current conext. Then wrap each of the collection components to consume that and re-start the Virtualizer context there. You can use whatever method of targeting you want. A specific context to each component, something akin to our slots architecture, etc.

    I'm going to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions