Skip to content

fix(Table): call the latest onSortChange from useTableSortableState - #11349

Merged
cixzhang merged 1 commit into
mainfrom
fix/sortable-render-reads
Nov 12, 2026
Merged

cixzhang merged 1 commit into
mainfrom
fix/sortable-render-reads

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

User impact

A table that uses useTableSortableState with a controlled onSortChange called an old handler. The header kept the handler from the last sort change. A parent that passed a new handler while the sort stayed the same, for example after a filter or page change, had its old handler called when the user sorted, and that handler acted on out-of-date state.

Problem and solution fit

  • Why it happens: the memoized sort config copied onSortChangeRef.current while rendering, in a memo keyed on the sort state. The copy is only as new as the last sort change.
  • Why it harms the task: sorting runs the parent's old logic, for example a stale page reset or out-of-date analytics.
  • Why that matters: the user sees a sort that ignores the parent's current state, with no error.
  • Fix: the config holds one stable handler that calls the latest onSortChange when the user sorts. In the same way, two other render-time reads of a latest-value ref now read the value directly (the comparators memo, and BottomSheetPanel's ignored snap points). Neither of those two changes behavior today; both remove a render read.

Expected behavior and authority

architecture:react-component-runtime: render stays pure, and a mutable ref is not the source of rendered output (INV4). The documented useTableSortableState contract is that the table calls the consumer's onSortChange, which means the current one.

Smallest restoration

  • useTableSortableState: onSortChange in the config is a stable useCallback that calls the latest handler. The config still does not change when a parent passes an inline callback.
  • useTableSortableState: the sorted-data memo reads comparators directly. Comparator identity still does not re-sort, because callers often pass an inline object. A comment on the dependency list says so.
  • BottomSheetPanel: the ignored-points message reads snapPoints directly. The memo stays keyed on the snap points' identity, and the resolver still reads its ref at call time.
  • Left as is: useTableSortable :227. The plugin's stable-identity contract needs an owner decision. The plugin is documented and tested as "referentially stable across renders", so header rendering can see the current sort only through a ref read during render.

New public API: none.

Evidence

  • Reproduction before the change: the new test renders with onSortChange={first}, re-renders with second and the same sort, then presses a header. On main, first was called (red).
  • Result after the change: second is called and first is not.
  • Representative unchanged path: the existing "does not re-sort when its parent re-renders with an inline comparators object" test still passes, and so do the BottomSheet snap-point suites.
  • Regression test: Table sortable and BottomSheet suites, 691 tests pass.

Scope

Components: Table, BottomSheet

  • 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

  • vitest run packages/core/src/Table/plugins/sortable packages/core/src/BottomSheet.
  • ESLint and Prettier on the touched files.
  • pnpm check:changesets.
  • no-comment-provenance.baseline.mjs --check.
  • pnpm review:pre: 0 findings, merges cleanly.

@vercel

vercel Bot commented Nov 12, 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 12, 2026 3:49am UTC

Request Review

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

astracat-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 12, 2026, 9:48 AM UTC

@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec 4451825

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 4451825

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
BottomSheet 69.4 kB (+6 B) · 7.7 kB CSS (0) — ✗ · ✓ · —
BottomSheetSwitcher 18.6 kB (0) · 3.4 kB CSS (0) — ✗ · ✓ · —
TableBody 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
27 more components
Component Size LoC Visual · A11y · RTL
TableFooter 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
TableHeader 3.5 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · ✗
TableRow 7.1 kB (0) · 4.0 kB CSS (0) — ✓ · ✓ · ✗
PowerSearch 299.3 kB (+3 B) · 24.7 kB CSS (0) — — · — · —
DateTimeInput 215.4 kB (+2 B) · 18.7 kB CSS (0) — — · — · —
DateInput 192.8 kB (+1 B) · 17.0 kB CSS (0) — — · — · —
TableCell 114.5 kB (+1 B) · 18.0 kB CSS (0) — ✓ · ✓ · ✓
Tokenizer 102.0 kB (+1 B) · 19.1 kB CSS (0) — — · — · —
lab/Comments 358.8 kB (+1 B) · 2.5 kB CSS (0) — — · — · —
lab/CommentThread 352.1 kB (+1 B) · 2.4 kB CSS (0) — — · — · —
Markdown 193.4 kB (0) · 20.8 kB CSS (0) — — · — · —
MultiSelector 99.7 kB (0) · 16.9 kB CSS (0) — — · — · —
Pagination 115.5 kB (0) · 17.8 kB CSS (0) — — · — · —
Selector 105.3 kB (0) · 17.4 kB CSS (0) — — · — · —
Table 130.4 kB (0) · 18.5 kB CSS (0) — ✓ · ✓ · —
TableHeaderCell 114.0 kB (0) · 17.7 kB CSS (0) — ✓ · ✓ · ✓
TableSelectionToolbar 73.7 kB (0) · 13.2 kB CSS (0) — ✓ · ✓ · ✓
lab/ChatEmojiPicker 110.7 kB (0) · 773 B CSS (0) — — · — · —
lab/ChatReactionBar 111.8 kB (0) · 1.1 kB CSS (0) — — · — · —
lab/CommentComposer 156.4 kB (0) · 1.9 kB CSS (0) — — · — · —
lab/TourStep 101.7 kB (0) · 590 B CSS (0) — — · — · —
richtext/RichTextEditorToolbar 284.4 kB (0) · 454 B CSS (0) — — · — · —
MoreMenu 102.5 kB (−1 B) · 17.6 kB CSS (0) — — · — · —
Popover 95.4 kB (−1 B) · 14.2 kB CSS (0) — — · — · —
ContextMenu 113.2 kB (−2 B) · 17.5 kB CSS (0) — — · — · —
DropdownMenu 102.3 kB (−3 B) · 17.6 kB CSS (0) — — · — · —
TimeInput 167.5 kB (−6 B) · 16.0 kB CSS (0) — — · — · —
Visual changes: 2 snapshots in 1 component

A change is a question for the reviewer, not a failure.

Component · story Before After Diff
BottomSheet · Text Only (neutral, dark) before frame after frame diff frame
BottomSheet · Text Only (neutral, light) before frame after frame diff frame

Package size: @astryxdesign/core 7.37 MB packed (+269 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.

Approved. CI passed on this head, including the new regression test.

The stable handleSortChange wrapper makes every sort activation call the parent's current onSortChange, and the new test pins the case where the handler changes while the sort stays the same. The comparators and snapPoints reads now follow the runtime rule that a latest-value ref is read only in callbacks, never during render or in useMemo.

  • Optional: the reworded sentence at BottomSheetPanel.tsx:824-826 is 28 words; the earlier inline suggestion splits it.
  • Follow-up, not for this pull request: once the committed-ref hook lands (#11352), onSortChangeRef and the other two refs here should be written through it instead of during render.
Claims and files checked
  • Sorting through the sort-state hook calls the caller's latest sort handler: verified (useTableSortableState.tsx:294-297, :317).
  • Before, a sort kept the handler from the last sort change, so a new handler passed without a sort change was skipped: verified against the base memo and the plugin's click path (useTableSortable.tsx:251-287).
  • The config keeps one stable handler and does not change for an inline callback: verified (useTableSortableState.tsx:321).
  • The comparators memo and the bottom sheet's ignored-points warning read their values directly and change no behavior: verified (useTableSortableState.tsx:301, BottomSheetPanel.tsx:834).
  • Comparator identity still does not re-sort, and a comment on the dependency list says so: verified (useTableSortableState.tsx:302-303; test at useTableSortableState.test.tsx:320-351).
  • The bottom sheet memo stays keyed on the snap points and its resolver reads the ref at call time: verified (BottomSheetPanel.tsx:839, :846).
  • The sort header's own ref read is left for a Table API decision: verified (useTableSortable.tsx:227).
  • No new public API: verified from the source diff.
  • The new test fails before the change and passes after it: it passes in CI on this head; the failure before the change is verified by reading the code.
  • The stated suite, lint and typecheck results: CI's test, lint and typecheck jobs passed on this head; the local pre-review run is not verified here.
  • Files: .changeset/table-sort-latest-handler.md clear; packages/core/src/BottomSheet/BottomSheetPanel.tsx clear, with the optional comment suggestion; packages/core/src/Table/plugins/sortable/useTableSortableState.test.tsx clear; packages/core/src/Table/plugins/sortable/useTableSortableState.tsx clear.

Automated review (maintainer link)

Comment on lines +824 to +826
// changed, so it must not churn on a re-render the drag caused. The resolver
// reads the points through a ref, so it resolves them at call time —
// against whatever the viewport is then, not what it was at memo time.

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: the reworded sentence runs 28 words, over the 25-word limit for a description in the source-comment rule, and covers two topics. For example:

  // changed, so it must not churn on a re-render the drag caused. The resolver
  // reads the points through a ref when it runs. It resolves them against the
  // viewport at that time, not at memo time.

@cixzhang
cixzhang marked this pull request as ready for review November 12, 2026 09:22
@cixzhang
cixzhang merged commit 8caa062 into main Nov 12, 2026
128 checks passed
@github-actions
github-actions Bot deleted the fix/sortable-render-reads branch November 13, 2026 06:03

This branch was successfully deployed

1 active deployment
Preview — 44518259 Deployed Nov 12, 2026 by vercel[bot]
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.

1 participant