fix: unnecessary setPageIndex to the last page when pageIndex is 0 #471
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The logic within this
useEffect
is designed to detect when thepageIndex
exceeds its valid range. If such an occurrence is detected, it resets to the last page. The secondif
statement within this logic, however, poses certain issues. If the current page is already the first one (withpageIndex
being 0) and thetotalRowCount
is 0 (indicating an empty table), thisif
statement would validate, but in fact, there is no need to invokesetPageIndex
to reset to the last page because the currentpageIndex
is already the final page. This unnecessary invocation ofsetPageIndex
not only creates redundant function calls but also leads to other issues.We utilize TanStack Query to request data:
const query = useQuery(...)
, and we then passquery.isLoading
into MantineReactTable. Due to the caching mechanism,isLoading
only remainstrue
during the initial query request; it isfalse
afterwards. Should the total returned by this query remain 0, it will trigger the aforementionedif
logic, causing any component subsequent to the query request to initiate an onPaginationChange (triggered by thesetPageIndex
method) upon mounting.