Hi there!
I just started learning React and I was exploring Material React Table V2 (https://www.material-react-table.com/).
I got stuck trying to update a mere element’s color within my table, and even though it seemed like a very simple problem, I spent so much time trying to solve it in vain.
Below is my code for the React component I created. I only want to change the color of the sorting and filter buttons to white.
Code:
import { useMemo } from "react";
import {
MaterialReactTable,
useMaterialReactTable,
type MRT_ColumnDef,
} from "material-react-table";
//example data type
type Property = {
pID: string;
address: string;
postCode: string;
llID: string;
tID: string;
};
//nested data is ok, see accessorKeys in ColumnDef below
const data: Property[] = [
{
pID: "101S",
address: "101 SunnyGo Property",
postCode: "00Z 00Y",
llID: "FKY",
tID: "CHP",
},
{
pID: "103L",
address: "103 Louvre de lion",
postCode: "SE14 6QJ",
llID: "PLN",
tID: "JTR",
},
{
pID: "109K",
address: "109 Konoha Village",
postCode: "HA2 8AB",
llID: "NRT",
tID: "SSK",
},
{
pID: "504S",
address: "504 Shibuya",
postCode: "UB2 5AQ",
llID: "SUK",
tID: "ITD",
},
{
pID: "789M",
address: "789 Marine Ford",
postCode: "BR2 8DN",
llID: "WB",
tID: "AC",
},
{
pID: "579E",
address: "579 Empel Down",
postCode: "EN6 5QD",
llID: "MG",
tID: "DMG",
},
{
pID: "154A",
address: "154 Amazon Lili",
postCode: "SE20 7YZ",
llID: "BH",
tID: "LFY",
},
{
pID: "269A",
address: "269 Alabasta",
postCode: "N4 2DN",
llID: "CD",
tID: "RBN",
},
{
pID: "963C",
address: "963 Clover Kingdom",
postCode: "ME4",
llID: "JLS",
tID: "YM",
},
];
const Table = () => {
//should be memoized or stable
const columns = useMemo<MRT_ColumnDef<Property>[]>(
() => [
{
accessorKey: "pID", //access nested data with dot notation
header: "P-ID",
size: 150,
},
{
accessorKey: "address",
header: "Address",
size: 200,
},
{
accessorKey: "postCode", //normal accessorKey
header: "Post Code",
size: 200,
},
{
accessorKey: "llID",
header: "LL-ID",
size: 150,
},
{
accessorKey: "tID",
header: "T-ID",
size: 150,
},
],
[]
);
const table = useMaterialReactTable({
columns,
data, //data must be memoized or stable (useState, useMemo, defined outside of this component, etc.)
columnFilterDisplayMode: "popover",
muiTableHeadRowProps: {
sx: {
backgroundColor: "#1f1f1f",
},
},
muiTableHeadCellProps: {
sx: {
color: "white",
"& .MuiSvgIcon-root, MuiTableSortLabel-root": {
color: "white !important",
},
},
},
});
return <MaterialReactTable table={table} />;
};
export default Table;