-
-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination issue when pagination on server #333
Conversation
Thanks for taking a stab at fixing this. I don't think you even need to re-calculate. Just detect if the paginationTotalRows changed then check the currentPage is > 1 and there are 0 records in the data.length or calculatedRows.length. then just move back a page
I would also use |
|
|
calculatedRows.length when using Since you are "server-side" paging the calculatedRows.length (or data.length) will only be whatever the total length of the data loaded from you api is. So, if you're paging for 10 records per page the length is 10 even though you may have hundreds or more of records. What needs to be done is when the data.length reaches 0 then we trigger the pagination back one page. i.e. you delete the last record on a page (data.length is 0) This is why All of this works differently in client-side pagination since we have the entire dataset loaded which i why the code uses calculatedRows.length for those calculations (it uses Array.slice to give you the effect of pagination) |
|
This is not working you can rerun the test that I have written. I think the use cases are
|
@@ -197,6 +197,18 @@ const DataTable = memo(({ | |||
handleChangePage(recalculatedPage); | |||
} | |||
|
|||
if (paginationServer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see https://reactjs.org/docs/hooks-overview.html#rules-of-hooks
Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@janhartmann - I forgot about that and totally agree. I think we need to re-think the design/implementation of useEffect hooks in DataTable. Also, we need to move these hooks into their own files/logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it as well, @jbetancur need to clean up that
Empty right now, but a placeholder for refactor work |
Strange thing is the hooks linter does not catch it |
I just mocked up an actual delete against a live API and it seems to be working. @sarathdr your logic solves the issue. Just need to get that test working. I'll have a look
|
Actually, if you want to just remove the tests/snapshot updates I can merge this in. I can add the test in the refactor I am doing |
Remember to put the “pagination” in the dependency array. Good rule is that all used variables within the hook needs to go into the dependency array, so if any of these change it will re-compute. |
Or not sure if needed in this case |
OK I will push after that |
Codecov Report
@@ Coverage Diff @@
## master #333 +/- ##
==========================================
+ Coverage 99.76% 99.77% +<.01%
==========================================
Files 40 40
Lines 432 439 +7
Branches 96 99 +3
==========================================
+ Hits 431 438 +7
Misses 1 1
Continue to review full report at Codecov.
|
I created a codesandbox of what I am testing locally. It will serve as a validation once 4.0.1 is cut https://codesandbox.io/s/react-data-table-server-side-pagination-delete-xmdju
|
@janhartmann not needed in this case. While it is good practice in general, this sort of update is specific to paginationTotalRows changing (ie, a remote delete where the API returns some different row count) we don't want the page check to fire if say pagination is togged to false |
@sarathdr Make sure you rebase from master as I just made some ordering changes to useEffect |
The pull request is closed, has this been merged ? Do you want me to re-open after rebase? |
Issue