How to create a table component using material-react-table #560
-
I want to create a table component using material-react-table but I don't know which data type I should use to define its props (such as columns and data). I will define the props in an interface structure. I am using Typescript as programming language. I used the MRT_ColumnDef as column type and an array of objects as data type and I know that they are incorrect. The columns can be defined using useMemo, for example,
and the data can be represented as an array of objects. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can define a re-usable MRT component that can take any data, but you will probably want to use a TypeScript generic. //extend MaterialReactTableProps so you don't have to type out 200 props yourself
interface Props<TData extends Record<string, any> = {}> extends MaterialReactTableProps<TData> {
columns: MRT_ColumnDef<TData>[];
data: TData[];
//etc.
}
export const YourCustomTableComponent = <TData extends Record<string, any> = {}>({
columns,
data,
...rest
}: Props<TData>) => {
//code... maybe custom columns...
return (
<MaterialReactTable
columns={columns}
data={data}
//your prop customizations
{...rest}
/>
);
} |
Beta Was this translation helpful? Give feedback.
You can define a re-usable MRT component that can take any data, but you will probably want to use a TypeScript generic.