Skip to content

fix(table-core): avoid no-op page index reset updates#6245

Open
HaukeSchnau wants to merge 1 commit intoTanStack:mainfrom
HaukeSchnau:fix/no-op-page-index-reset
Open

fix(table-core): avoid no-op page index reset updates#6245
HaukeSchnau wants to merge 1 commit intoTanStack:mainfrom
HaukeSchnau:fix/no-op-page-index-reset

Conversation

@HaukeSchnau
Copy link
Copy Markdown

@HaukeSchnau HaukeSchnau commented May 7, 2026

🎯 Changes

Fixes #6243.

resetPageIndex() now returns early when the clamped reset target matches the current page index. This prevents automatic page-index resets from emitting redundant pagination updates when row-model memo invalidation occurs while the table is already on the reset page.

The change keeps real resets intact, including clamping through pageCount, but avoids calling setPageIndex()/setPagination() for no-op resets.

Added table-core regression coverage for:

  • direct resetPageIndex() no-op behavior
  • real reset behavior from a non-zero page
  • queued _autoResetPageIndex() no-op behavior
  • the core row-model invalidation path that triggers _autoResetPageIndex() after data changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

Local verification run instead:

  • pnpm --filter @tanstack/table-core test:types
  • pnpm --filter @tanstack/table-core test:lib
  • pnpm --filter @tanstack/react-table test:types
  • pnpm --filter @tanstack/react-table test:lib
  • pnpm --filter @tanstack/table-core build
  • pnpm --filter @tanstack/react-table build

🚀 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

    • Optimized pagination page-index reset to prevent unnecessary state updates when the table is already at the target page.
  • Tests

    • Added test coverage for pagination reset behavior.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 652e9e04b3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +284 to +289
const currentPageIndex =
table.getState().pagination?.pageIndex ?? defaultPageIndex

if (currentPageIndex === pageIndex) {
return
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove stale-state guard from resetPageIndex

The new early return in resetPageIndex reads table.getState().pagination.pageIndex before deciding whether to emit onPaginationChange, but in controlled usage that state is often one render behind. If setPageIndex and resetPageIndex are called in the same event (or an auto-reset runs before the controlled state update is applied), this guard can incorrectly treat the reset as a no-op and skip the reset update, leaving pagination on the non-reset page. Previously, resetPageIndex always emitted the reset updater and avoided this stale-read race.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. I updated the fix to avoid the stale controlled-state read in resetPageIndex().

The revised version lets resetPageIndex() always enqueue the reset via setPageIndex(), makes unchanged setPageIndex() updates return the existing pagination object, and updates makeStateUpdater() to return the existing table state when a slice updater returns the existing slice. That preserves queued updater semantics such as setPageIndex(1); resetPageIndex() while still allowing React to bail out on true no-op auto-resets.

I also added a regression test for that queued update/reset case.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5d74be71-eba1-46a0-a61b-d90569545b28

📥 Commits

Reviewing files that changed from the base of the PR and between 961258c and 54f11e8.

📒 Files selected for processing (4)
  • .changeset/quiet-page-reset.md
  • packages/table-core/src/features/RowPagination.ts
  • packages/table-core/src/utils.ts
  • packages/table-core/tests/RowPagination.test.ts

📝 Walkthrough

Walkthrough

This PR optimizes pagination behavior to avoid emitting unnecessary state updates when the page index is reset to its current value. The changes centralize equality checks in the state updater utility and add page-index clamping logic to prevent redundant state object allocations, with comprehensive tests validating state identity preservation.

Changes

Pagination State Identity Optimization

Layer / File(s) Summary
State Update Equality Check
packages/table-core/src/utils.ts
makeStateUpdater now computes the updated value into newValue, checks Object.is equality against the prior state field, and returns the existing old state object when unchanged to avoid unnecessary state object creation.
Pagination Page-Index Clamping
packages/table-core/src/features/RowPagination.ts
New getSafePageIndex helper centralizes page-index bounds checking based on pageCount (treating undefined/-1 as unbounded). setPageIndex refactored to clamp using the helper and return the existing pagination state when the clamped index matches old.pageIndex.
Pagination State Identity Tests
packages/table-core/tests/RowPagination.test.ts
New test suite validates that pagination state object identity is preserved when resetting to the current page index, confirms resets from different pages trigger state changes, verifies queued reset behavior after updates, and tests identity preservation during auto-reset with data changes.
Release Documentation
.changeset/quiet-page-reset.md
Changeset entry documents patch version bump for @tanstack/react-table and @tanstack/table-core, describing the pagination behavior change.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Hops through the state with gentle care,
Comparing old with new so fair,
When pages match, no change is made—
State identity will not fade!
No render freeze, just peace and speed,

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: avoiding no-op page index reset updates, which directly addresses the core issue being fixed.
Linked Issues check ✅ Passed The PR implements the stated objective from issue #6243: centralizing page-index clamping logic and avoiding redundant pagination updates when already on the reset page, with comprehensive regression tests.
Out of Scope Changes check ✅ Passed All changes are within scope: the refactored clamping logic, state equality check, and test suite directly support the no-op reset optimization without introducing unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@HaukeSchnau HaukeSchnau force-pushed the fix/no-op-page-index-reset branch from 652e9e0 to 54f11e8 Compare May 8, 2026 06:40
@HaukeSchnau
Copy link
Copy Markdown
Author

@codex review

@HaukeSchnau
Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant