I am using React + Material Table.
What to achieve
I want to have the option to set pageSize to 'All' to match to total count of rows in material-table.
What I have done
I use useState to create rowsPerPage , and set rowsPerPage in the handleChangeRowsPerPage function. I use component overriding to customize the Pagination and pass rowsPerPage and handler to its props. I also pass rowsPerPage to MaterialTable's pageSize option.
Problem I encounter
The page does not re-size, and the state.pageSize does not update. Even though the props.options.pageSize and Pagination successfully update with onChangeRowsPerPage.
Link to sandbox
import React, { useState } from "react";
import MaterialTable from "material-table";
import { TablePagination } from "@material-ui/core";
export default function ComponentOverriding() {
const [rowsPerPage, setRowsPerPage] = useState(5);
//updates pagination, but no re-size
const handleChangeRowsPerPage = event => {
//enable to set rows per page to match total count
event.target.value === "All"
? setRowsPerPage(data.length)
: setRowsPerPage(event.target.value);
};
const data = [
{ name: "Mehmet", surname: "Baran", birthYear: 1987 },
{ name: "Zerya Betül", surname: "Baran", birthYear: 2017 }
];
return (
<MaterialTable
title="Component Override Demo"
columns={[
{ title: "Name", field: "name" },
{ title: "Surname", field: "surname" },
{ title: "Birth Year", field: "birthYear" }
]}
data={data}
//options.pageSize updates, but no re-size
options={{ pageSize: rowsPerPage }}
// onChangeRowsPerPage={handleChangeRowsPerPage} //TypeError Cannot read property 'value' of undefined
components={{
Pagination: props => (
<TablePagination
{...props}
rowsPerPageOptions={[5, 10, 20, "All"]}
rowsPerPage={rowsPerPage}
//updates pagination, but no re-size
onChangeRowsPerPage={handleChangeRowsPerPage}
/>
)
}}
/>
);
}