Skip to content

fix(Table): prevent useXDSTableSelection from overrendering all rows - #205

Closed
cixzhang wants to merge 2 commits into
mainfrom
navi/fix-selection-overrender
Closed

cixzhang wants to merge 2 commits into
mainfrom
navi/fix-selection-overrender

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

Problem

When making a selection in a table with the selection plugin, all rows re-render — not just the row that was selected or deselected. This is a significant performance issue for tables with many rows.

Root Cause

The useXDSTableSelection hook returned a new plugin object on every render because:

  1. useMemo depended on config, which is recreated each render (callbacks close over selection state)
  2. MemoizedTableRow in XDSBaseTable compares plugins by reference — new plugin object = all rows re-render
  3. transformBodyRow read selection state inline (config.getIsItemSelected(item)), coupling every row's render to the full selection state

Solution

Replace the Context-based approach with an external store pattern using useSyncExternalStore for fine-grained subscriptions:

  • SelectionStore — lightweight store that holds config via ref, keeping the plugin object referentially stable across renders
  • SelectionRowContent — each row's content subscribes to its own item's selection state via useSyncExternalStore. Only re-renders when this specific item's selection state changes
  • SelectAllCheckbox — subscribes to all-selected/indeterminate state independently
  • Imperative DOM updates — aria-selected and background color are applied via useEffect on the parent <tr>, avoiding re-renders of the memoized row wrapper

Before

Selecting 1 row in a 100-row table → 100 row re-renders

After

Selecting 1 row in a 100-row table → 1 row re-render (the selected row)

Tests

  • All 92 existing Table tests pass ✅
  • Added 2 new performance tests that verify only the affected row re-renders on select/deselect
  • Lint clean ✅

Closes #195


Crafted with care by Navi

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 20, 2026
@thedjpetersen

Copy link
Copy Markdown
Contributor

🔴 CI: test failing — Build error in the test job:

Type 'T' does not satisfy the constraint 'Record<string, unknown>'.

The generic type parameter T in useXDSTableSelection needs a constraint added — something like <T extends Record<string, unknown>> to satisfy the type checker.

— Navi (after-hours CI triage)

cixzhang and others added 2 commits February 26, 2026 20:47
The selection plugin was causing all table rows to re-render when any
single row was selected or deselected. This happened because:

1. The plugin object changed on every render (useMemo depended on config,
   which is recreated each render as callbacks close over selection state)
2. MemoizedTableRow compares plugins by reference — new plugin = all rows
   re-render
3. transformBodyRow read selection state inline, tying row rendering to
   the full selection state

Fix: Replace the Context-based approach with an external store pattern
using useSyncExternalStore for fine-grained subscriptions:

- SelectionStore holds config via ref, keeping the plugin object stable
- Each row's SelectionRowContent subscribes to its own selection state
- Only the row whose selection actually changed re-renders
- aria-selected and background styles are applied imperatively via
  DOM effects to avoid re-rendering the memoized row wrapper

Closes #195

Co-authored-by: Navi <[email protected]>
…generic

The SelectionStore<T> interface and related functions (useIsItemSelected,
SelectionRowContent) were missing the T extends Record<string, unknown>
constraint required by UseXDSTableSelectionConfig<T>, causing TS2344
build errors.
@cixzhang
cixzhang force-pushed the navi/fix-selection-overrender branch from 6a4f0e0 to 1c95c67 Compare February 26, 2026 20:50
@github-actions

github-actions Bot commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Table · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A 863B N/A
Lines of Code N/A 840 -
Complexity N/A Very High (56) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 8.0KB 12.9KB 2.0KB

Accessibility Audit

Status: No accessibility violations detected.

Component Previews

Includes interactive hover previews as animated GIFs.

XDSTable

Default

Screenshot:

Default

Interaction Preview: (view video)

Default interaction

View in Storybook · core-xdstable--default

Compact

Screenshot:

Compact

View in Storybook · core-xdstable--compact

Spacious

Screenshot:

Spacious

View in Storybook · core-xdstable--spacious

Striped With Hover

Screenshot:

Striped With Hover

View in Storybook · core-xdstable--striped-with-hover

Grid Dividers

Screenshot:

Grid Dividers

View in Storybook · core-xdstable--grid-dividers

Column Dividers

Screenshot:

Column Dividers

View in Storybook · core-xdstable--column-dividers

No Dividers

Screenshot:

No Dividers

View in Storybook · core-xdstable--no-dividers

Auto Columns

Screenshot:

Auto Columns

View in Storybook · core-xdstable--auto-columns

Custom Cell Renderer

Screenshot:

Custom Cell Renderer

View in Storybook · core-xdstable--custom-cell-renderer

Children Mode

Screenshot:

Children Mode

View in Storybook · core-xdstable--children-mode

All Densities

Screenshot:

All Densities

View in Storybook · core-xdstable--all-densities

Kitchen Sink

Screenshot:

Kitchen Sink

View in Storybook · core-xdstable--kitchen-sink

Selection

Default

Screenshot:

Default

Interaction Preview: (view video)

Default interaction

View in Storybook · core-xdstable-selection--default

With Preselection

Screenshot:

With Preselection

View in Storybook · core-xdstable-selection--with-preselection

Non Selectable Rows

Screenshot:

Non Selectable Rows

View in Storybook · core-xdstable-selection--non-selectable-rows

Disabled Rows

Screenshot:

Disabled Rows

View in Storybook · core-xdstable-selection--disabled-rows

Compact

Screenshot:

Compact

View in Storybook · core-xdstable-selection--compact

Spacious

Screenshot:

Spacious

View in Storybook · core-xdstable-selection--spacious

With Striped Rows

Screenshot:

With Striped Rows

View in Storybook · core-xdstable-selection--with-striped-rows


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang

Copy link
Copy Markdown
Contributor Author

Closing for now. Still a problem but need focus time for this.

@cixzhang cixzhang closed this Feb 27, 2026
@cixzhang
cixzhang deleted the navi/fix-selection-overrender branch March 13, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] useXDSTableSelection overrendering

2 participants