Skip to content

autoResetPageIndex resets to initialState pageIndex instead of 0 #6207

@federicogildemuro

Description

@federicogildemuro

TanStack Table version

v8.21.3

Framework/Library version

React v18.3.1

Describe the bug and the steps to reproduce it

When autoResetPageIndex is enabled (or defaults to true), the page index resets to initialState.pagination.pageIndex instead of 0. This is problematic when initialState is used to restore a page from the URL (e.g., deep linking to page 5) — applying a filter that reduces results resets to page 5 instead of page 0, leaving the user on an invalid/empty page.

Steps to reproduce:

  1. Set initialState.pagination.pageIndex to a non-zero value (e.g., restored from URL params)
  2. Leave autoResetPageIndex as default (true) or explicitly set it to true
  3. Change the data (e.g., apply a filter that reduces total pages below the initial page)
  4. Observe that the page index resets to initialState.pagination.pageIndex instead of 0

Root cause:

In RowPagination.ts, _autoResetPageIndex calls table.resetPageIndex() without passing true:

// RowPagination.ts - _autoResetPageIndex
table.resetPageIndex() // resets to initialState, not 0

And resetPageIndex defaults to initialState when called without defaultState = true:

table.resetPageIndex = defaultState => {
  table.setPageIndex(
    defaultState
      ? defaultPageIndex                                              // 0
      : table.initialState?.pagination?.pageIndex ?? defaultPageIndex  // initialState or 0
  )
}

Suggested fix:

Change _autoResetPageIndex to call table.resetPageIndex(true) so it always resets to 0.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

const table = useReactTable({
  data,
  columns,
  initialState: {
    pagination: { pageIndex: 5, pageSize: 10 }, // restored from URL
  },
  autoResetPageIndex: true,
  getCoreRowModel: getCoreRowModel(),
  getPaginationRowModel: getPaginationRowModel(),
});
// When data changes (e.g., after filtering), page resets to 5 instead of 0

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

Yes, I think I know how to fix it and will discuss it in the comments of this issue

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions