reactjs - MUI DataGrid 替换分页 - validateDOMNesting(...):不能作为子级出现
问题描述
我正在替换 Material-UI 制作的 DataGrid 组件中的分页并得到以下控制台错误:Warning: validateDOMNesting(...): <td> cannot appear as a child of <div>
我不认为我在做任何特别的事情,所以我想知道问题到底是什么。我是否使用了错误的分页组件?
这是一个重现问题的CodeSandbox。
我部分使用了 MUI、React 和 Typescript 的 v5。
import { TablePagination } from "@mui/material";
import { DataGrid, GridColumns } from "@mui/x-data-grid";
export default function App() {
const columns: GridColumns = [
{
field: "foo"
}
];
const rows = [
{
id: "bar"
}
];
return (
<div style={{ height: 500 }}>
<DataGrid
columns={columns}
rows={rows}
components={{ Pagination: TablePagination }}
componentsProps={{
pagination: {
count: 1,
page: 0,
onPageChange: () => {},
rowsPerPage: 10,
rowsPerPageOptions: [10, 25, 50, 100],
onRowsPerPageChange: () => {},
labelRowsPerPage: "Zeilen pro Seite",
labelDisplayedRows: () => `Seite ${1} von ${1}`,
nextIconButtonProps: {
disabled: true
}
}
}}
/>
</div>
);
}
解决方案
TablePagination
默认组件是td
mui-datagrid 使用 div 而不是table / tr / td
标签,所以你必须说组件是div
:
componentsProps={{
pagination: {
count: 1,
page: 0,
component: "div", // here
onPageChange: () => {},
rowsPerPage: 10,
rowsPerPageOptions: [10, 25, 50, 100],
onRowsPerPageChange: () => {},
labelRowsPerPage: "Zeilen pro Seite",
labelDisplayedRows: () => `Seite ${1} von ${1}`,
nextIconButtonProps: {
disabled: true
}
}
}}
推荐阅读
- c# - 使用 WebApi C# 分层架构从数据库中获取 1 个对象的最佳实践
- php - mysql查询显示最喜欢的帖子
- node.js - 网站和 nodejs 应用程序的 NGINX 配置
- ajax - 如何将类添加到数据表中除第一列之外的所有表列
- npm - Gulp v4 监视任务
- driver - 如何安装 avssamp 和 avshws 驱动程序?
- php - 创建了行,但值未插入表中
- c# - 更新与另一个表对象 (NxN) 相关的实体对象(插入、删除)
- scala - Spark RDD 操作通过键对值求和
- python - 如何从列表中删除与同一列表中的其他字符串具有相同单词集(以混乱顺序)的字符串