如何在 MUI DataGrid 中将附加属性传递给 renderCell?

问题描述 投票:0回答:0

我想将额外的道具(来自父组件的状态)传递给MUI DataGrid的

renderCell
函数,但在仔细阅读MUI文档后我不知道该怎么做。

例如,我的父组件有一个状态

displayMode
来指示整个组件是否处于亮/暗/其他模式,并且其所有子组件都应该接收此状态作为道具,以根据模式值执行/显示不同的操作.

此父组件下的 DataGrid 也应该从父组件接收此状态作为 prop,并且我必须使

renderCell
访问此 prop 来渲染内容(每行一个按钮,单击时会将某些效果切换到当前行) ) 取决于
displayMode
.

我无法使用 DataGrid 的

initialState
,因为根据 MUI 文档,DataGrid 不会对此更改做出反应,并且
displayMode
是父组件中的可更改状态。任何帮助将不胜感激,谢谢!

例如当前配置:

// MUI DataGrid column format
const columns: GridColDef[] = [
    ...,
    {
        field: 'renderColorAction',
        headerName: 'Color Button',
        flex: 0.75,
        renderCell: RenderColorActionButtonCell,
    },
    ...
]

// This is a React Component, I can use hooks here
// `GridRenderCellParams` is the MUI DataGrid's param
// `IRowData` is the type for whole row
function RenderColorActionButtonCell(
        params: GridRenderCellParams<IRowData, any, any, GridTreeNodeWithRender>) {
    return (<xxx>);
}

我想要实现的目标与下面非常相似:

// `IPropFromParent` is an expected type for passed-in props from parent component
// which should contain the `displayMode` so that I can access parent's state, to
// do rendering conditionally depends on parent's state
function RenderColorActionButtonCell(
        params: GridRenderCellParams<IRowData, any, any, GridTreeNodeWithRender>,
        props: IPropFromParent) {
    const { displayMode }  = props;
    ...
    return (<xxx>);
}
javascript reactjs typescript material-ui datagrid
© www.soinside.com 2019 - 2024. All rights reserved.