重写material-table分页组件触发Material-UI控制台警告该如何解决?
问题原因
你自定义覆盖分页组件时,透传给MTablePagination的subProps里包含了MaterialTable上层传入的classes对象,其中selectRoot、caption两个类名键是MTablePaginationInner组件不支持的,MUI的类名校验机制检测到不匹配的键就会抛出警告,该警告不影响功能运行,仅会在开发环境控制台显示。
解决方案
透传参数前过滤subProps里的classes属性,仅保留MTablePaginationInner支持的root键即可,修改后的代码如下:
<MaterialTable columns={columns} data={invoices} icons={IconsForMaterialTable} options={{ paging:true}} components={{ Pagination: (subProps) => { // 过滤classes,仅保留组件支持的root属性 const filteredClasses = { root: subProps.classes?.root } return <Box display="flex" justifyContent="flex-end"> <Box width="260px" justifyContent="flex-end"> <MTablePagination {...subProps} classes={filteredClasses} count={pageCount} onChangePage={(e, page) => setPage(page)} page={page} rowsPerPage={rowsPerPage} /> </Box></Box> }}} />
如果后续出现其他同类多余类名警告,按照相同逻辑在filteredClasses里只保留警告提示中允许覆盖的属性即可。
内容的提问来源于stack exchange,提问作者anish
相关产品推荐
相关产品推荐

