Customizing TableRow Hover Effects in React with Material-UI

I've been spinning in circles with this issue. It seems like I just can't grasp how class overrides function in Material-ui.

I attempted to follow the advice in this response: CSS on Material UI components However, it didn't produce any results (no errors). This could be due to me working with a stateful component rather than the stateless one used in the example.

Currently, I am experimenting with the following code...

<TableRow className={styles.tableRow}
    key={n.id} hover
    onClick={event => this.handleRowClick(event, n.id)}
    classes={{ 'hover': {color: '#7EA5FF'}}} >
      <TableCell>&nbsp;</TableCell>
      <TableCell>{n.title}</TableCell>
      <TableCell>{n.author}</TableCell>
</TableRow>

However, I encounter this error message: the key hover provided to the classes property is not valid for TableRow. You need to provide a non-empty string instead of: [object Object].

Any assistance would be greatly appreciated. Thank you.

Answer №1

Looking at the code snippet you provided, it appears that you have:

className={styles.tableRow}

followed by

classes={{ 'hover': {color: '#7EA5FF'}}

To properly modify your styles, you should include your style adjustments within the declaration of styles.tableRow and remove the className prop as it does not seem to be supported in the API. You can refer to the Material-UI documentation for more information: https://material-ui.com/api/table-row/

Your code should look something like this:

const styles = theme => ({
     tableRow: {
      hover: {
         /// your styles...
        }
     }
});
....

render() {
 // It is recommended to use the classes prop, which corresponds to 
 // the styling obtained from the withStyles HOC function of MUI
 const classes = this.props.classes;
 return (
   <TableRow
    classes={classes.tableRow}
    key={n.id} hover
    onClick={event => this.handleRowClick(event, n.id)}
>
      <TableCell>&nbsp;</TableCell>
      <TableCell>{n.title}</TableCell>
      <TableCell>{n.author}</TableCell>
</TableRow>
 );
}

If you provide the full code of your component in a sandbox or similar tool, I can offer more assistance. I need to see how you declare the styles variable and how you pass it to the component.

Material-UI relies on JSS for styling components, which may be unfamiliar at first.

For a helpful example relevant to your situation, check out: https://material-ui.com/demos/tables/#customized-tables

Additionally, here is a useful documentation link: https://material-ui.com/customization/overrides/#overriding-with-classes

Answer №2

const styles = makeStyles((theme) => ({
  hoverEffect: {
    "&:hover": {
      backgroundColor: "green !important",
    },
  },
}));

 const customStyles = styles();
<TableRow applyHover={true} customClasses={{hoverEffect: customStyles.hoverEffect,}}/>

Answer №3

const customStyles = makeStyles((theme) => ({
rowHighlight: {
    "&:hover": {
      backgroundColor: '#7EA5FF'
    }
  }
}));

const highlightClasses = customStyles();

<HoverRow hover className={highlightClasses.rowHighlight}>

Answer №4

My implementation utilizes the withStyles (Version 4) feature, which relies on a Higher-Order Component approach. For an additional example, refer to this link.

const StyledTableRow = withStyles((theme) => ({
  root: {
    '&:hover': {
      backgroundColor: 'red',
    },
  },
}))(TableRow);

Subsequently,

<StyledTableRow>
   // Custom row logic implemented here
</StyledTableRow>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieving an IE7 opacity background while keeping the text unaffected

This is the given HTML code structure (view in JS Fiddle) How can I adjust the background color to be 20% opaque white while keeping the text black? It needs to be compatible with IE7. <ul class="menu"> <li class="first expanded active-trail ...

Integration issue: React.js is failing to render within a Django project

I have been working on a project where React is not rendering anything on Django localhost. index.html <!DOCTYPE html> <html lang="en"> <head></head> <body> <div id="App"> <!---all will b ...

Is it possible to transform an array of objects into a new array of objects?

After searching for a similar question, I realized none of them really addressed my issue... EDIT: My main goal is to create a reusable dropdown component where I can pass items as props directly. These items need to be objects with both a key (for the fi ...

Create a continuous integration pipeline utilizing Azure DevOps platform

During my attempt to create a React application, I encountered an error while building the Azure pipeline using Azure DevOps. View the error image from the Azure pipeline in Azure DevOps The code for the azure-pipeline.yml file can be found in my GitHub ...

Recursive React components. String iteration enhancement

In my React app, I am trying to create a string hierarchy from an object. The object structure is like this: { name: 'name1', parent:{ name: 'name2', parent:{ name: 'name3', parent: null }}} My plan is to use a state variable ...

Tips for avoiding content appearing beneath overlapping elements

I am facing an issue with a hero image that has an overlapping box. I want to add content below the box but it ends up appearing behind the box. How can I ensure that the content shows below the box while maintaining responsiveness? .shell { display: ...

Utilize Material-UI's <Autocomplete /> feature to conduct searches using multiple parameters

Recently, I started delving into mastering Material UI and something has been on my mind. We are working with an array of objects as follows: const top100Films = [ { label: 'The Shawshank Redemption', year: 1994 }, { label: 'The Godfath ...

Is the CSS content-visibility property compatible with background images?

Can CSS content-visibility and contain-intrinsic-size also be used with the background-image property, or do they only work with IMG tags? Will either of these properties have any impact on the following code snippet? <span id="image"> #i ...

What specific features does CSS3 offer in terms of background-size properties?

Let's delve into my knowledge: $, like in font-size: 14$; em, as in margin: 2em; What other units are out there? ...

Prevent automatic refreshing of React app on remote devices

My friend and I are collaborating on the development and testing of a React app simultaneously. He accesses the app from my machine using my IP address but encounters an issue where every time I save something in the code, the app reloads on both of our ...

How can I prevent having to repeat the getStaticProps function on multiple pages within NextJS?

Navigating React/NextJS as a newcomer has been a challenge, especially when it comes to handling file imports and requirements. For my project, I am utilizing getStaticProps for Static Site Generation (SSG) to fetch data from Prismic CMS. Referencing the ...

Transferring a variable from a child component to a parent component within Next.js

I am faced with a challenge involving two components, home and tiny. The tiny component is imported inside the home component as shown in the code snippet. I am attempting to pass the value of value.toString("html") from tiny.js to home.js. If direct passi ...

Can someone explain how to use JavaScript to make table data fill the entire row in a table?

After clicking the button, the layout of the table changes. I want to keep the original table layout even after the JavaScript function runs. I am still learning about JavaScript. function toggle(button) { if(document.getElementById("1").value=="Show ...

Having trouble with setting the margin-top of a div?

When I attempted to apply the margin-top property to a div's style, it didn't have any effect. https://i.stack.imgur.com/jyY2G.png Below is the code snippet: .side { margin-left: 65%; margin-top: 3%; margin-right: 3%; border: 1px sol ...

Displaying a segment of information extracted from a JSON array

I'm currently tackling a project that involves using React, Redux, and TypeScript. Within the JSON file, there is a key-value pair: "data_start": "2022-09-02" Is there a way to display this date in a different format, specifical ...

Using jQuery to create a Select All Checkbox that toggles a specific class

I have come across similar examples in the past, but I have been unable to make it work as intended. The examples I found only use standard checkboxes. I attempted to customize the checkbox using a sprite sheet by applying a class, but I am struggling to a ...

How to style a div's height responsively behind a circle using CSS

Can a CSS pattern be created with responsive design, ensuring that the background line is always in the correct position and height regardless of window size? https://i.sstatic.net/27Tf2.jpg ...

Challenges faced when dealing with MongoDB and the latest version of webpack

Struggling to navigate MongoDB and React.js, encountering issues with MongoDB dependencies. Here are the dependencies listed in package.json: "dependencies": { "dotenv": "^16.3.1", "mongodb": "^4.1.0", &qu ...

Learn how to stream videos using the YouTube Player API's loadPlaylist feature

Is there a way to make the next video play automatically using the loadPlaylist option? I've tried implementing this code but unfortunately, it doesn't work and the video won't play: <div id="player"></div> <script> var ...

Challenges arise when trying to access environment variables using react-native-dotenv in React

I am currently working on two separate projects, one being an app and the other a webapp. The app project is already set up with react-native-dotenv and is functioning as expected. However, when I attempt to use the same code for the webapp, I encounter an ...