fix(svelte-table): keep controlled state from resetting after each change #6641
main fromrefs/pull/6641/head·opened 🎯 Changes#
Fixes #6640.
Bug cause reported in full in the accompanying #6640 issue. TLDR: The Svelte adapter keeps the table's options in a deep $state. Every time the options are applied, Svelte wraps the plain data array in a new proxy, so table.options.data is a different object each time even though the data hasn't changed. The core row model treats that as new data and runs its auto-resets. Changing controlled expanded or pagination applies the options again, so each change is immediately reset back. When data is already in $state, Svelte reuses the existing proxy and the bug doesn't show.
Fix:
$state.raw. set always replaces the whole options object, so a top-level signal is all the store needs. As a side effect, row.original is the caller's object again rather than a proxy of it.
Tests: PaginationHarness gains a "Next page" button, and a new ExpandingHarness controls expanded with createTableState. The existing pagination test only calls setPageSize(3), which ends on page 0 whether or not the reset fires, so it didn't catch this. Both new tests fail before the change and pass after it.
The examples don't show the bug because they all keep data in $state. Svelte leaves an existing proxy as it is, so table.options.data stays stable there. The bug needs a plain array (a constant, a $derived value, or a TanStack Query result), which is why the new tests use the fixtures' plain arrays. The TanStack Query example avoids it for a different reason: it sets manualPagination: true, which turns off the page reset.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm testandpnpm test:e2e, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
Locally: the @tanstack/svelte-table targets from pnpm test (test:lib, test:types, test:eslint, build, test:build) all pass. test:e2e passes for all 41 Svelte examples (164 tests). The two new unit tests fail without the change and pass with it.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Fixed option updates resetting controlled pagination and expanded-row state in Svelte tables.
- Tests
- Added coverage confirming pagination advances to the expected rows and expanded rows remain displayed.
- 1
NickSpag(GitHub profile, opens in a new tab) changed the title
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/table
- Branch: main
- File: .coderabbit.yaml
- Commit: c14e022
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
ba239b28-c9b7-46e7-9e58-ac2723e87c83
Commits-
Reviewing files that changed from the base of the PR and between 15141a83a276d2111326c6f370d222ac0c7a670e and b25c4dce1d140a9fae43fd659e5dd27d5423b9a6.
Files selected for processing: 1-
packages/svelte-table/src/reactivity.svelte.ts
Files skipped from review as they are similar to previous changes: 1-
- packages/svelte-table/src/reactivity.svelte.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
Walkthrough
-
The Svelte adapter now stores its options value with
$state.raw. Lifecycle tests cover controlled pagination and expansion changes.Changes: Svelte options state
Priority:Layer / File(s) Summary Store options as raw state
packages/svelte-table/src/reactivity.svelte.ts,.changeset/raw-svelte-options-store.mdcreateRuneWritableAtominitializes its value with$state.raw. The changeset records the options-store change.Exercise controlled state changes
packages/svelte-table/tests/fixtures/ExpandingHarness.svelte,packages/svelte-table/tests/fixtures/PaginationHarness.svelte,packages/svelte-table/tests/adapter-lifecycle.test.tsThe fixtures provide controls for expansion and pagination. Lifecycle tests assert the displayed row IDs after those changes. NormalEstimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
riccardoperra
Merge Risk:
Minimal · up tob25c4-
The change addresses controlled pagination and expansion resets, with no identified merge-blocking risk.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title clearly and concisely describes the main fix: preventing controlled Svelte table state from resetting after changes. Description check The description includes the required Changes, Checklist, and Release Impact sections. It explains the cause, fix, tests, issue reference, and changeset status. All checklist items are addressed. Linked Issues check Directly linked issue #6640requires that controlled expanded and pagination changes persist whendatais a plain array, and that genuine data changes still trigger resets. The whole-PR diff (merge …Out of Scope Changes check All changed files connect to the #6640objective. The source change is the one-line$stateto$state.rawswitch increateRuneWritableAtom, with an explanatory doc comment that cites#6640. The `…Full details: Linked Issues check
-
Explanation
Directly linked issue
#6640requires that controlled expanded and pagination changes persist whendatais a plain array, and that genuine data changes still trigger resets. The whole-PR diff (merge base c14e0221 to head b25c4dce) changescreateRuneWritableAtomin packages/svelte-table/src/reactivity.svelte.ts from$state(initialValue)to$state.raw(initialValue). This atom backs the options store.setreplaces the whole options object, so a raw signal keeps the caller'sdataarray identity across unchanged option syncs. The core row model memoization therefore does not recompute, and the auto-resets do not run. A new data array still changes identity and still triggers resets, so the genuine-change behavior remains. Therow.originalidentity requirement in the issue scope is also met by the same change. Automated tests were added.adapter-lifecycle.test.tsadds 'createTableState-controlled pagination is not reset by its own change' (clicks 'Next page', expects '5,6,7,8,9') and 'createTableState-controlled expanded state is not reset by its own change' (clicks 'Expand first row' in the new ExpandingHarness, expects '1'). The ExpandingHarness fixture uses a plain constantdataarray andcreateTableState, which matches the issue reproduction. I did not execute the tests. The fail-before and pass-after results come from the PR description and are unverified by this review.
Full details: Out of Scope Changes check
-
Explanation
All changed files connect to the
#6640objective. The source change is the one-line$stateto$state.rawswitch increateRuneWritableAtom, with an explanatory doc comment that cites#6640. The.changeset/raw-svelte-options-store.mdpatch changeset describes that published-package change. The test changes support the regression coverage: the 'Next page' button in PaginationHarness, the new ExpandingHarness fixture, and the two new lifecycle tests. The change torow.originalidentity is the behavior the issue asks to fix. I found no unrelated refactoring or dependency change in the whole-PR changed files I inspected. I did not read the full PaginationHarness diff or the full changeset diff, and I rely on the summary for those two files.
-
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.-
NickSpag(GitHub profile, opens in a new tab) linked this to fix #6640
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: b25c4dce1d140a9fae43fd659e5dd27d5423b9a6
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR