Ant Design Table pagination state synchronization during dynamic total updates
0 reputation · 01 Oct 2025, 18:14 UTC
0 reputation · 01 Oct 2025, 18:14 UTC
In Ant Design 5.x, the Table component handles large datasets through server-side pagination by controlling the current and pageSize properties. This architecture relies on the pagination prop to communicate the total record count returned by the backend.
A challenge arises when external filters or query bounds modify the total number of available records. If the current page index exceeds the new maximum page count calculated from the updated total, the component's internal state and the displayed pagination UI may become desynchronized.
What is the recommended mechanism to force the Table component to reset the current page to one when the total property is updated via an external filter? Does the component provide a built-in synchronization trigger for this scenario, or must the state be managed manually through the current prop?
31275 reputation · 02 Oct 2025, 01:27 UTC
Ant Design’s Table does not automatically reset current when total changes. The component only recalculates the page count; it keeps the existing current value. Therefore, to force a jump to the first page after an external filter updates total, you must explicitly set current to 1 in your pagination state.
pagination object is fully controlled by the props you pass.total alone will recalc the number of pages but leave current untouched.current exceeds the new maximum page number, the pager will still display the out‑of‑range value until you adjust it.const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
const [total, setTotal] = useState(0);
const [data, setData] = useState([]);
const fetchData = async (pageNum, size) => {
const res = await api.fetch({ page: pageNum, size });
setData(res.items);
setTotal(res.total); // <‑ total updated by filter
};
// React effect: reset page when total changes
useEffect(() => {
if (page > Math.ceil(total / pageSize)) {
setPage(1); // force first page
}
}, [total, pageSize]);
const handleTableChange = (pagination) => {
setPage(pagination.current);
setPageSize(pagination.pageSize);
fetchData(pagination.current, pagination.pageSize);
};
return (
<Table
dataSource={data}
pagination={{
current: page,
pageSize,
total,
onChange: handleTableChange,
}}
/>
);
Ant Design’s Table has no built‑in “reset‑on‑total‑change” flag. The component assumes that the parent controls the pagination state. Hence the only reliable trigger is to update the current prop yourself.
If the table still jumps to page 1 unexpectedly after you set current to 1, verify that you are not inadvertently resetting current elsewhere in your component (for example, by re‑initializing the state on every render or by passing an uncontrolled pagination object). Ensuring that the pagination prop is a stable object will keep the UI in sync.
Use comments to ask for clarification. Post a solution as an answer.
31,275 reputation · 02 Oct 2025, 02:15 UTC
In Ant Design 5.x the Table pagination is controlled through the pagination.current and pagination.pageSize props. Changing total alone recalculates the page count but does not move current, so an out-of-range page can render an empty body.
A practical distinction is when the reset happens. Resetting current to 1 in the filter-change handler before the next fetch keeps the request and the UI in sync and avoids a brief empty render that occurs when clamping current after the response arrives. If pagination is left uncontrolled, the only external reset is a remount via a changing key, which also clears sorters, filters and scroll state.