Repository navigation
fix(Table): sort only when the data or the sort changes - #10942
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
| 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
There was a problem hiding this comment.
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
dataand the sort stay the same keeps the old row order untildatachanges, whereasmainhas 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:runlabel. - Optional: the comment above
COLLATOR_OPTIONScould name the constraint it protects; see the inline suggestion.
[Automated review]
| // One options object, so the collator, and the sorted data with it, survive | ||
| // a render that changes neither the locale nor the sort. |
There was a problem hiding this comment.
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.|
/approve-spec 60e22db |
There was a problem hiding this comment.
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_OPTIONScould name the constraint it protects; see the earlier inline suggestion on that line.
[Automated review]
|
/approve-spec 16ac69c |
There was a problem hiding this comment.
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_OPTIONScould name the constraint it protects; see the earlier inline suggestion on that line.
useTableSortableStatere-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}).useCollatormemoizes 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
useCollatormemoizes the collator on the locale and the options object's identity.Smallest restoration
One module constant,
COLLATOR_OPTIONS, inuseTableSortableState.tsx. No API, visual or default changes. The collator's options are the same.Evidence
Scope
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.tsxrun whole against main's hook: the new test fails, and the other 20 pass.@astryxdesign/coretypecheck: 0 errors. ESLint and Prettier on the changed files: clean.#8403touches the sortable plugin, and merges cleanly with this change.