SortModelChange上的Material-UI数据网格导致无限循环 [英] Material-UI Data Grid onSortModelChange Causing an Infinite Loop
本文介绍了SortModelChange上的Material-UI数据网格导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循排序模型文档(https://material-ui.com/components/data-grid/sorting/#basic-sorting),并且使用的sortModel
和onSortModelChange
与文档中使用的完全相同。但是,我在加载页面后立即得到一个无限循环(我可以根据console.log判断这一点)。
我尝试的内容:
- 在onSortChange属性中使用useCallback
- 使用服务器端排序(https://material-ui.com/components/data-grid/sorting/#server-side-sorting)
- 在onSortChange函数内使用
if (sortModel !== model) setSortModel(model)
。
我总是以同样的问题告终。我正在使用Blitz.js。
我的代码:
useState:
const [sortModel, setSortModel] = useState<GridSortModel>([
{
field: "updatedAt",
sort: "desc" as GridSortDirection,
},
])
行定义:
const rows = currentUsersApplications.map((application) => {
return {
id: application.id,
business_name: application.business_name,
business_phone: application.business_phone,
applicant_name: application.applicant_name,
applicant_email: application.applicant_email,
owner_cell_phone: application.owner_cell_phone,
status: application.status,
agent_name: application.agent_name,
equipment_description: application.equipment_description,
createdAt: formattedDate(application.createdAt),
updatedAt: formattedDate(application.updatedAt),
archived: application.archived,
}
})
列定义:
const columns = [
{ field: "id", headerName: "ID", width: 70, hide: true },
{
field: "business_name",
headerName: "Business Name",
width: 200,
// Need renderCell() here because this is a link and not just a string
renderCell: (params: GridCellParams) => {
console.log(params)
return <BusinessNameLink application={params.row} />
},
},
{ field: "business_phone", headerName: "Business Phone", width: 180 },
{ field: "applicant_name", headerName: "Applicant Name", width: 180 },
{ field: "applicant_email", headerName: "Applicant Email", width: 180 },
{ field: "owner_cell_phone", headerName: "Ownership/Guarantor Phone", width: 260 },
{ field: "status", headerName: "Status", width: 130 },
{ field: "agent_name", headerName: "Agent", width: 130 },
{ field: "equipment_description", headerName: "Equipment", width: 200 },
{ field: "createdAt", headerName: "Submitted At", width: 250 },
{ field: "updatedAt", headerName: "Last Edited", width: 250 },
{ field: "archived", headerName: "Archived", width: 180, type: "boolean" },
]
呈现数据网格并使用sortModel/onSortChange
<div style={{ height: 580, width: "100%" }} className={classes.gridRowColor}>
<DataGrid
getRowClassName={(params) => `MuiDataGrid-row--${params.getValue(params.id, "status")}`}
rows={rows}
columns={columns}
pageSize={10}
components={{
Toolbar: GridToolbar,
}}
filterModel={{
items: [{ columnField: "archived", operatorValue: "is", value: showArchived }],
}}
sortModel={sortModel}
onSortModelChange={(model) => {
console.log(model)
//Infinitely logs model immediately
setSortModel(model)
}}
/>
</div>
提前感谢!
推荐答案
我通过将rows
和columns
包装在useRef中修复了此问题,并对它们使用了它们的.current
属性。立即修复。
这篇关于SortModelChange上的Material-UI数据网格导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文