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.pageIndex to a non-zero value (e.g., restored from URL params)
- Leave
autoResetPageIndex as default (true) or explicitly set it to true
- 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.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
TanStack Table version
v8.21.3
Framework/Library version
React v18.3.1
Describe the bug and the steps to reproduce it
When
autoResetPageIndexis enabled (or defaults totrue), the page index resets toinitialState.pagination.pageIndexinstead of0. This is problematic wheninitialStateis 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:
initialState.pagination.pageIndexto a non-zero value (e.g., restored from URL params)autoResetPageIndexas default (true) or explicitly set it totrueinitialState.pagination.pageIndexinstead of0Root cause:
In
RowPagination.ts,_autoResetPageIndexcallstable.resetPageIndex()without passingtrue:And
resetPageIndexdefaults toinitialStatewhen called withoutdefaultState = true:Suggested fix:
Change
_autoResetPageIndexto calltable.resetPageIndex(true)so it always resets to0.Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
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