How to have different color for alternate row for MUI v4 Table?

Viewed 882

Below are my code. I tried setting the tableRowRoot in the style.js but the row background color for alternate row did not change. I hope to just set the root for table row so I do not need to add any class or logic to the rows etc. Hope you guys can show me the correct way to do this.

import { makeStyles } from '@material-ui/core/styles';

export default makeStyles((theme) => ({
  tableRowRoot: {
    '&:nth-of-type(odd)': {
      backgroundColor: theme.palette.action.hover,
    },
  },
}));
import useStyles from './styles';

const Event = ({ pendingEvents, handleUpdateEvent }) => {
  const classes = useStyles();

  return (
    <Container className={classes.content}>
      <div className={classes.toolbar} />
      <Typography className={classes.title} variant='h3' gutterBottom>
        List of Pending Request
      </Typography>
      <TableContainer component={Paper}>
        <Table className={classes.table} aria-label='simple table'>
          <TableHead>
            <TableRow>
              <TableCell className={classes.th}>Email</TableCell>
              <TableCell className={classes.th}>Name</TableCell>
              <TableCell className={classes.th}>Reason</TableCell>
              <TableCell className={classes.th}>Start Date</TableCell>
              <TableCell className={classes.th}>End Date</TableCell>
              <TableCell className={classes.th}>Approve / Reject</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {pendingEvents.map((event) => (
              <TableRow key={event.id}>
                <TableCell component='th' scope='row'>
                  {event.email}
                </TableCell>
                <TableCell>{event.name}</TableCell>
                <TableCell>{event.reason}</TableCell>
                <TableCell>{new Date(event.start).toDateString()}</TableCell>
                <TableCell>{new Date(event.end).toDateString()}</TableCell>
                <TableCell>
                  <IconButton
                    aria-label='Approve'
                    color='inherit'
                    onClick={() => handleUpdateEvent(event, 'approved')}
                  >
                    <ThumbUpIcon color='primary' />
                  </IconButton>
                  <IconButton
                    aria-label='Reject'
                    color='inherit'
                    onClick={() => handleUpdateEvent(event, 'rejected')}
                  >
                    <ThumbDownIcon color='secondary' />
                  </IconButton>
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </Container>
  );
};

export default Event;
1 Answers

You have a tableRowRoot rule, but I don't see you set the className in the TableRow to apply the custom styles:

<TableRow key={event.id} className={classes.tableRowRoot} /* <------- Add this */>

From the docs, you can also use withStyles to create a styled component from the original one:

const StyledTableRow = withStyles((theme) => ({
  root: {
    '&:nth-of-type(odd)': {
      backgroundColor: theme.palette.action.hover,
    },
  },
}))(TableRow);
<TableBody>
  {pendingEvents.map((event) => (
    <StyledTableRow key={event.id}>
      {...}
    </StyledTableRow>
  ))}
</TableBody>
Related