Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][TablePagination] Out of range error #40234

Closed
2 tasks done
lokomass opened this issue Dec 18, 2023 · 6 comments
Closed
2 tasks done

[material-ui][TablePagination] Out of range error #40234

lokomass opened this issue Dec 18, 2023 · 6 comments
Labels
component: TablePagination The React component. package: material-ui Specific to @mui/material

Comments

@lokomass
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

Link to live example: https://codesandbox.io/p/sandbox/zealous-perlman-forked-yyqttv

Steps:

  1. go to page 2
  2. remove the 11th item
  3. trigg the error

Current behavior 馃槸

No response

Expected behavior 馃

no error

Context 馃敠

When I remove an item in a page, and this is the last (like 11th element), I want to go to previous page.
But, there is an error because on page 1, there isn't no more items.

Your environment 馃寧

No response

@lokomass lokomass added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 18, 2023
@zannager zannager added the component: TablePagination The React component. label Dec 18, 2023
@aacevski
Copy link
Contributor

Adding this piece of code in the TablePagination component fixes this issue:

 React.useEffect(() => {
    const newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
    if (page > newLastPage) {
      onPageChange(null, newLastPage);
    }
  }, [count, page, rowsPerPage, onPageChange]);

However should this fix be implemented in the component itself? If so, I can open a pull request for this fix - however in my honest opinion it would pollute the component itself. What do you guys think?

@lokomass
Copy link
Author

Thanks for your reply, I've tried this in the sandbox : https://codesandbox.io/p/sandbox/zealous-perlman-forked-9kskcl
But problem is the same, we come back to first page, but Mui error appears again

@aacevski
Copy link
Contributor

Yep, this happens because of line 374 in the TablePagination.js component.

image

I would propose to add the useEffect fix in the component itself (in the core component I mean) and remove this warning since that use case will be handled with the useEffect.

@lokomass
Copy link
Author

You mean in the node_module folder ??? There isn't another solution ?

@aacevski
Copy link
Contributor

You mean in the node_module folder ??? There isn't another solution ?

No no, I mean I meant to add this code in the core package so it gets fixed for you, but that was more of a question to the maintainers haha, sorry for the confusion.

@danilo-leal danilo-leal changed the title Failed prop type: MUI: The page prop of a TablePagination is out of range (0 to 0, but page is 1). [material-ui][Pagination] Out of range error Dec 25, 2023
@danilo-leal danilo-leal added the package: material-ui Specific to @mui/material label Dec 25, 2023
@ZeeshanTamboli
Copy link
Member

You should address this in your component, which is why we issue the warning. Previously, we handled it in the Table Pagination component, but we removed that functionality in #14534. You can find the reasoning behind this decision in the discussion in #13995.

Furthermore, I don't believe you need to use useEffect to handle it. You can calculate it during component re-renders. Using useEffect for this purpose is inefficient. You can learn more in the React docs.

Check out the updated CodeSandbox: here.

@ZeeshanTamboli ZeeshanTamboli removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 3, 2024
@mbrookes mbrookes changed the title [material-ui][Pagination] Out of range error [material-ui][TablePagination] Out of range error Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: TablePagination The React component. package: material-ui Specific to @mui/material
Projects
None yet
Development

No branches or pull requests

6 participants