Skip to content

Table: TableStateContext changes identity every render, so ResizableTableContainer re-renders every cell on width changes #10759

Description

@RobHannay

Provide a general summary of the issue here

Inside ResizableTableContainer, every change to the table's width re-renders every visible Cell, even though nothing a cell reads has changed. The container stores the width in state, so each width change re-renders TableInner. The TableStateContext value TableInner provides is a new object on every render, so every cell re-renders.

This is easy to hit when a layout animates the width, for example a sidebar with transition: width 0.2s. A width change lands on every frame, and each one re-renders every cell.

🤔 Expected Behavior?

When only the table width changes, cells should not re-render. Only the layout should update.

😯 Current Behavior

TableInner keeps expandedKeys in its own useControlledState and passes them to useTableState as a controlled prop. useTableState then creates a new Set on every render:

let [expandedKeys, setExpandedKeys] = useControlledState(
  props.expandedKeys ? new Set(props.expandedKeys) : undefined,
  // ...
);

So state.expandedKeys has a new identity on every render. On top of that:

  • useTableState returns a new object literal each time.
  • UNSTABLE_useFilteredTableState returns {...state, collection, selectionManager: state.selectionManager.withCollection(collection)}.

As a result, TableStateContext changes identity on every TableInner render, and every Cell reads it.

I instrumented TableInner during a width animation:

  • New on every render: the TableStateContext value and its selectionManager.
  • Stable: the collection, the cell nodes, the drag-and-drop context and the selection state.
  • Of the state's fields, only expandedKeys changed identity.

Measured: one sidebar open or close (about 13 width changes) in a production build with a virtualized Table, react-aria-components 1.21.1 and React 19.2. Frame drops are at 4x CPU throttling in Chrome, median of 4 interleaved runs per arm.

Cell renders per toggle Dropped frames per toggle (4x CPU)
As is 2,772 14.5
TableStateContext value memoized in TableInner (local patch) 378 5.5
No ResizableTableContainer 440 2

Most of the remaining gap to no container is a second layout per width change. The container measures its width in a ResizeObserver and calls setWidth. So the Virtualizer first lays out with the old columnWidths, then lays out again once the container's state lands.

💁 Possible Solution

  1. In useTableState, stop creating a new Set every render, for example useMemo(() => props.expandedKeys ? new Set(props.expandedKeys) : undefined, [props.expandedKeys]).
  2. Memoize the objects returned by useTableState and UNSTABLE_useFilteredTableState, including selectionManager.withCollection(collection), on their inputs. Then TableStateContext keeps its identity when nothing has changed.

My local patch only did (2) inside TableInner, memoizing the value on the collection, the selection state fields, the sort descriptor and TableInner's own expandedKeys. That alone gave the 2,772 → 378 above.

🔦 Context

We wanted to keep ResizableTableContainer so we can add column resizing later. For now we've removed it, because the cost lands on every frame of our nav and side panel animations.

🖥️ Steps to Reproduce

  1. Render a virtualized Table (a few hundred rows, about 10 columns) inside ResizableTableContainer, inside a parent whose width animates with a CSS transition.
  2. Toggle the width and count Cell renders, or record a performance profile.
  3. Every visible cell re-renders on every frame of the transition.

Version

react-aria-components 1.21.1, react-stately 3.50.0. Still present on main (740c6c5).

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

macOS

Activity

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