Skip to content

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

Merged
cixzhang merged 1 commit into
mainfrom
navi/fix/table-selection-perf
Mar 31, 2026
Merged

cixzhang merged 1 commit into
mainfrom
navi/fix/table-selection-perf

Conversation

@cixzhang

@cixzhang cixzhang commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When selecting a row, all rows re-render — not just the selected one.

Root Cause

  1. useMemo depended on config, recreated each render (closures over selection state)
  2. MemoizedTableRow compares plugins by reference — new plugin = all rows re-render
  3. transformBodyRow read selection state inline, coupling every row to full state

Solution

External store pattern via useSyncExternalStore:

  • SelectionStore — config via ref, plugin object referentially stable
  • SelectionRowContent — per-row subscription, only re-renders on own state change
  • SelectAllCheckbox — independent subscription
  • Imperative DOM — aria-selected + background via useEffect on <tr>

Before: select 1 row in 100 → 100 re-renders
After: select 1 row in 100 → 1 re-render

Tests

  • 100/100 table tests pass
  • 2 new perf tests verify isolated row re-renders
  • Lint clean

Revives #205 (closed for focus time). Closes #195


@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 31, 2026
@cixzhang
cixzhang force-pushed the navi/fix/table-selection-perf branch 2 times, most recently from 786a1e2 to b4fc604 Compare March 31, 2026 04:48
Replace Context-based approach with an external store pattern using
useSyncExternalStore for fine-grained row subscriptions:

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

Before: selecting 1 row in 100-row table = 100 row re-renders
After: selecting 1 row in 100-row table = 1 row re-render

Adds 2 perf tests verifying only the affected row re-renders.
All 100 existing table tests pass.

Closes #195

Co-authored-by: Navi <[email protected]>
@cixzhang
cixzhang force-pushed the navi/fix/table-selection-perf branch from b4fc604 to 43f41b0 Compare March 31, 2026 05:14
@github-actions

github-actions Bot commented Mar 31, 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 940B N/A
Lines of Code N/A 893 -
Complexity N/A Very High (66) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 11.4KB 18.1KB 2.8KB

Accessibility Audit

Status: No accessibility violations detected.


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

@cixzhang
cixzhang merged commit c0d407c into main Mar 31, 2026
14 checks passed
@cixzhang
cixzhang deleted the navi/fix/table-selection-perf branch April 9, 2026 14:33
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

1 participant