Skip to content

fix(Table): sort only when the data or the sort changes - #10942

Merged
cixzhang merged 2 commits into
meta:mainfrom
vjeux:fix/table-sort-collator-once
Nov 11, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
vjeux:fix/table-sort-collator-once

Conversation

@vjeux

@vjeux vjeux commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

useTableSortableState re-sorted every row on every render of its owner. Red-first: yes, "does not re-sort when its parent re-renders with an inline comparators object" fails on main.

User impact

People with sortable tables built on useTableSortableState. Any render of the component that owns the sort state re-sorted all rows, even with the same data and sort. On a large table, every unrelated state change paid for a full sort.

Problem and solution fit

The hook called useCollator({numeric: true}). useCollator memoizes on its options object's identity, so a new object on every render made a new collator. The sorted data depends on the collator, so it re-sorted each time.

The options are now one module constant. Sorting runs only when the data, the sort or the locale changes.

Expected behavior and authority

  • useCollator memoizes the collator on the locale and the options object's identity.
  • The hook's own note: its comparators are held in a ref to avoid re-sorting on an inline comparators object. A new collator on every render undid that.

Smallest restoration

One module constant, COLLATOR_OPTIONS, in useTableSortableState.tsx. No API, visual or default changes. The collator's options are the same.

Evidence

  • Reproduction before the change: the new test fails on main b6b0c68, with the whole test file run.
  • Result after the change, at 60e22db: it passes.
  • Representative unchanged path: the existing sorting tests still pass, including custom comparators, numeric sorting and multi-sort.
  • Regression test or other mutation-sensitive proof: the test counts comparator calls across a parent re-render, and fails with the inline options restored.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

All runs below are at this PR's head, 60e22db.

The test renders a sortable table under a parent that passes an inline comparators object, sorted by salary. It counts comparisons, re-renders the parent with unrelated state, and expects no new comparison. Main makes 5.

  • pnpm exec vitest run packages/core/src/Table --project ui: 41 files, 769 tests pass.
  • useTableSortableState.test.tsx run whole against main's hook: the new test fails, and the other 20 pass.
  • @astryxdesign/core typecheck: 0 errors. ESLint and Prettier on the changed files: clean.
  • The open #8403 touches the sortable plugin, and merges cleanly with this change.
  • History: main merged at 7e7fbae on Nov 11, with no conflicts, so CI runs on a current base. The change's own diff is unchanged.

useTableSortableState passed a new options object to useCollator on
every render, so it got a new collator each time. Its sorted data
depends on the collator, so every render of its owner re-sorted all
rows, even with the same data and sort.

The collator options are now one module constant. Sorting runs only
when the data, the sort or the locale changes.
@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 11, 2026 5:39pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 10, 2026
@astracat-bot

astracat-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 11, 2026, 5:45 PM UTC

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 16ac69c

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
TableBody 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
TableFooter 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
TableHeader 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
5 more components
Component Size LoC Visual · A11y · RTL
TableRow 7.1 kB (0) · 4.0 kB CSS (0) — ✓ · ✓ · ✗
Table 130.0 kB (0) · 18.4 kB CSS (0) — ✓ · ✓ · —
TableCell 114.1 kB (0) · 18.0 kB CSS (0) — ✓ · ✓ · ✓
TableHeaderCell 113.7 kB (0) · 17.7 kB CSS (0) — ✓ · ✓ · ✓
TableSelectionToolbar 73.5 kB (0) · 13.3 kB CSS (0) — ✓ · ✓ · ✓

Package size: @astryxdesign/core 7.35 MB packed (+165 B); no new dependencies.

Touched components checked; add ci:run-visual, ci:run-a11y or ci:run-rtl to run the full suites.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approval waits on one engineering-owner decision; there are no blocking code findings.

The fix does what it says: the new test fails against main's hook (5 comparator calls on an unrelated re-render) and passes here.

  • Owner decision: at this head a comparator that changes its ordering while data and the sort stay the same keeps the old row order until data changes, whereas main has re-sorted on the next render since v0.4.6, so an engineering owner (the author is one) should confirm on this pull request whether that ships as a patch [fix] under AST-017 FR5 or needs a [breaking] changeset.
  • CI: it has not run on this head; it starts once the pull request leaves draft and a maintainer adds the ci:run label.
  • Optional: the comment above COLLATOR_OPTIONS could name the constraint it protects; see the inline suggestion.

[Automated review]

Comment on lines +236 to +237
// One options object, so the collator, and the sorted data with it, survive
// a render that changes neither the locale nor the sort.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, comment style: this sentence is a fragment and leaves out the constraint it protects, that useCollator keys its memo on the options object's identity. A shorter version:

// useCollator keeps its collator while this object stays the same.
// A new object on each render re-sorts every row.

@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 60e22db

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: CI has not run on this head yet. It runs once the pull request is marked ready for review and a maintainer adds the ci:run label.

The fix does what it says: the new render cost test fails against main's hook (5 comparator calls on an unrelated re-render) and passes here, and the other Table tests still pass. An engineering owner accepted the one behavior consequence as a patch fix: a comparator that changes its ordering while data and the sort stay the same now keeps the old row order until data changes, as the hook's docs describe.

  • Optional: the comment above COLLATOR_OPTIONS could name the constraint it protects; see the earlier inline suggestion on that line.

[Automated review]

@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 10, 2026
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 16ac69c

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI: CI has not run on this head yet. The pull request is still a draft and already carries ci:run, so marking it ready for review starts CI.

The fix does what it says: the new render cost test fails against main's hook (5 comparator calls on an unrelated re-render) and passes here, and the Table tests still pass. An engineering owner accepted the one behavior consequence as a patch fix: a comparator that changes its ordering while data and the sort stay the same keeps the old row order until data changes, as the hook's docs describe.

  • Optional: the comment above COLLATOR_OPTIONS could name the constraint it protects; see the earlier inline suggestion on that line.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as ready for review November 11, 2026 18:04
@cixzhang
cixzhang merged commit f2293a5 into meta:main Nov 11, 2026
129 checks passed

This branch was successfully deployed

1 active deployment
Preview — 16ac69c3 Deployed Nov 11, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) 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.

2 participants