-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
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:
- Set
initialState.pagination.pageIndexto a non-zero value (e.g., restored from URL params) - Leave
autoResetPageIndexas default (true) or explicitly set it totrue - Change the data (e.g., apply a filter that reduces total pages below the initial page)
- Observe that the page index resets to
initialState.pagination.pageIndexinstead of0
Root cause:
In RowPagination.ts, _autoResetPageIndex calls table.resetPageIndex() without passing true:
// RowPagination.ts - _autoResetPageIndex
table.resetPageIndex() // resets to initialState, not 0And 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 0Screenshots 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.