7487 commented on code in PR #39661:
URL: https://github.com/apache/superset/pull/39661#discussion_r3891740330
##########
superset-frontend/plugins/plugin-chart-table/src/DataTable/DataTable.tsx:
##########
@@ -244,6 +244,15 @@ export default typedMemo(function DataTable<D extends
object>({
...tableHooks,
);
+ // Clamp pageIndex when filtered data shrinks below current view (#31403).
+ // pageCount is derived from the filtered rows, so zero rows always means
+ // pageCount === 0 and is handled by the second branch.
+ if (pageCount > 0 && pageIndex >= pageCount) {
+ gotoPage(pageCount - 1);
+ } else if (pageCount === 0 && pageIndex !== 0) {
+ gotoPage(0);
+ }
Review Comment:
The render-phase call here is intentional. This is React's documented
"adjusting state while rendering" pattern (see [You Might Not Need an
Effect](https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes)):
`gotoPage` dispatches to the `useReducer` instance inside this same
component's `useTable`, which React explicitly supports during render — the
"Cannot update a component while rendering" warning only fires for
cross-component updates, and the guard conditions make the update converge in
one extra pass (after the clamp, neither branch can fire again).
Moving it to a `useEffect` would reintroduce the bug this PR fixes: the
out-of-range `pageIndex` would render and commit once before the effect runs,
which is exactly the frame where the pagination model blows up (`currentPage
shouldn't be greater than totalPages`). Clamping during render ensures no child
ever sees the invalid index. The new unit tests run without any
update-during-render warnings.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]