The custom styles in Material-Table are taking precedence over all other styling, including Material UI styles, and causing icons to not

I recently started using the Material-Table component for a project I'm working on, and it's been great for managing tables with features like adding, editing, deleting, and searching rows. However, I've run into a few issues that I need help with.

The main issue I'm facing is related to styling. When I integrate Material UI components into my page as child components, all custom and built-in styling seems to disappear. For example, the padding and spacing between AppBar buttons are missing, and the font styling looks off.

Another problem I'm encountering is with icons. The icons in the table are not rendering properly; they just show up as large cut-off text instead of the expected images.

Interestingly, these styling and icon issues only occur when the table is present on the page. Other pages without the table do not seem to be affected by these issues.

If anyone has encountered similar problems or knows how to solve them, I would greatly appreciate any advice or suggestions. Thank you!

In an attempt to fix the icon display problem, I have already tried re-installing the library and importing the necessary resources. Below is a snippet of the code I used to implement the Material Table:

<MaterialTable
  title="Editable Example"
  columns={state.columns}
  data={state.data}
  actions={[
    {
      icon: 'edit',
      tooltip: 'Edit Study',
      onClick: (event, rowData) => alert("Do you want to edit " + rowData.name + "?") 
    },
    rowData => ({
      icon: 'clear',
      tooltip: 'Delete User',
      onClick: (event, rowData) => alert("You want to delete " + rowData.name), 
      disabled: rowData.birthYear < 2000
    })
  ]}
  editable={{
    onRowAdd: newData =>
      new Promise(resolve => {
        setTimeout(() => {
          resolve();
          const data = [...state.data];
          data.push(newData);
          setState({ ...state, data });
        }, 600);
      }),
    onRowDelete: oldData =>
      new Promise(resolve => {
        setTimeout(() => {
          resolve();
          const data = [...state.data];
          data.splice(data.indexOf(oldData), 1);
          setState({ ...state, data });
        }, 600);
      }),
  }}
/>

Answer №1

If you're experiencing issues with icons not appearing, the solution is to include the following code snippet:

import { forwardRef } from 'react';

import AddBox from '@material-ui/icons/AddBox';
import ArrowDownward from '@material-ui/icons/ArrowDownward';
import Check from '@material-ui/icons/Check';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import ChevronRight from '@material-ui/icons/ChevronRight';
import Clear from '@material-ui/icons/Clear';
import DeleteOutline from '@material-ui/icons/DeleteOutline';
import Edit from '@material-ui/icons/Edit';
import FilterList from '@material-ui/icons/FilterList';
import FirstPage from '@material-ui/icons/FirstPage';
import LastPage from '@material-ui/icons/LastPage';
import Remove from '@material-ui/icons/Remove';
import SaveAlt from '@material-ui/icons/SaveAlt';
import Search from '@material-ui/icons/Search';
import ViewColumn from '@material-ui/icons/ViewColumn';

const tableIcons = {
    Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
    Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
    Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
    Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
    DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
    Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
    Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
    Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
    FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
    LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
    NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
    PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
    ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
    Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
    SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
    ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
    ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
  };

<MaterialTable
  icons={tableIcons}
  ...
/>

For more details, refer to the official documentation: https://github.com/mbrn/material-table

Answer №2

If you are currently using Material-UI v5, there's a chance that it may not be fully compatible with the version of material-table you have installed. This could be causing your Material-UI styles to be overridden.

To resolve this issue and ensure compatibility with Material-UI v5, consider installing the material-table core by running the following command:

npm install @material-table/core@next

Alternatively, you can also use yarn to add the core of material-table with support for Material-UI v5:

yarn add @material-table/core@next

For more information, refer to this link: material-table with @material-ui v5

Answer №3

In my current scenario, I am utilizing @material-ui/[email protected], while material-table is operating on version 4.2.1.

The logging information can be obtained once material-table is installed

info Direct dependencies
├─ @material-ui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b7bba6b194e0fae6fae5">[email protected]</a>
└─ <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08d8194859289818ccd9481828c85a0d1ced4d0ced1">[email protected]</a>
info All dependencies
...

Therefore, I decided to upgrade material ui to @material-ui/[email protected] by running the command

yarn add @material-ui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="beddd1ccdbfe8a908c908f">[email protected]</a>
. This update resolved the issue for me.

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

After downloading the latest version of NodeJS, why am I seeing this error when trying to create a new React app using npx?

After updating to a newer version of NodeJS, I attempted to create a new React app using the command npx create-react-app my-app. However, I encountered the following error message: Try the new cross-platform PowerShell https://aka.ms/pscore6 PS E:\A ...

Tips for integrating semantic HTML with React components

As a newcomer to react, I am eager to establish a strong foundation before delving deep into the language and risking having to backtrack later on. I am curious about how to incorporate semantic HTML in react. Elements such as <header>, <nav>, ...

What is the best way to save an array of objects from an Axios response into a React State using TypeScript?

Apologies in advance, as I am working on a professional project and cannot provide specific details. Therefore, I need to describe the situation without revealing actual terms. I am making a GET request to an API that responds in the following format: [0: ...

What could be causing the error in my SVG path tag attributes?

Why am I encountering a react error with these attributes? stroke-linecap="square" stroke-linejoin="round" Should they be written as strokeLinecap and strokeLinejoin? <svg width="36" ...

Encountering a TypeError while trying to import grapesjs into a nextjs project, specifically receiving the error: "Cannot read properties of null (reading 'querySelector')

I encountered an issue while trying to integrate grapesjs into a nextjs project. The error I received was TypeError: Cannot read properties of null (reading 'querySelector') It appears that grapesjs is looking for the "#gjs" container by its id ...

If a cell in the MUI datagrid fails validation, revert the changes back to the original when the user clicks out of it

Currently utilizing a MUI datagrid for email field validation, the revert functionality works successfully but only triggers upon pressing the escape key. To achieve this behavior, I made modifications to the MUI validation example provided, where a toolti ...

When is it appropriate to utilize props and CSS in customizing Material UI components?

As a beginner with Material UI, I'm curious about when to use props within the .jsx script and when to utilize CSS in a separate stylesheet to style MUI elements. Is it necessary to still incorporate CSS stylesheets for MUI elements or is it preferabl ...

Flip the direction of this vertical CSS motion blur effect

Is there a way to modify this code so that the object can have a vertical motion blur instead of a horizontal one? I want it to move from top to bottom and then back up. Can anyone assist me with this? http://jsfiddle.net/db8gr4y6/ #outer { posit ...

The authentication process in ExpressJS with Passport.js is failing to return the res.user object in React

I recently followed a tutorial on Medium about implementing Passport js with a MERN stack. While I was successful in getting authentication to work, I am facing challenges with persisting users between routes. Below are excerpts from the code I used: ...

Struggling with customizing the style of react mui-datatables version 4

Currently, I am utilizing "mui-datatables": "^4.2.2", "@mui/material": "^5.6.1", and have attempted to customize the styling in the following manner: Refer to the Customize Styling official documentation for more details // CUSTOMIZING MUI DATATABLES imp ...

Error occurs while attempting to configure

I am facing some confusion with the usage of aws-sdk in a React project. I have been trying to configure it, but encountered an error that has left me puzzled. Oddly enough, when I checked the object debugger everything seemed fine. However, as soon as I t ...

Tips for disabling viewport resizer while accessing the Console panel in Chrome using Control+Shift+J

Currently, I am utilizing the viewport resizer in Chrome to preview how my code appears on various devices. However, I have encountered an issue - whenever I try to access the console using ctrl + shift + j, the viewport resizer opens instead. You can obs ...

How can the MUI Popover be initiated from the current position of the mouse cursor?

Is there a way to create a Material UI Popover that opens and closes when the mouse is pressed, with the opening animation starting from the position of the mouse at the time it was clicked? ...

Looking to position the Secondary Navigation Bar on squarespace at the bottom of the page, distinct from the primary Navigation Bar

When SALES PAGE becomes the secondary navigation option Instructions for positioning style to Bottom Center I am attempting to place it at the bottom of the navigation bar. Can you provide me with the necessary code or styles in squarespace? When I choose ...

The appearance of responsive CSS is altered when deployed compared to when it is viewed on the

I'm a beginner in web development and facing an issue with my mobile CSS. The strange thing is that when I view it on localhost, everything looks great, but once deployed (tried both Heroku and GitHub), it appears distorted. Even when I make extreme c ...

Leveraging Forms for Entering Google Maps Information

Recently, I've been working on an app that aims to generate a custom map based on user input from a form. If you'd like to test the functionality yourself, head over to this page. After filling out all required fields and hitting "Submit", the g ...

styling not affecting SVGIcon component in MaterialUI React

I am experiencing an issue with applying a style to the ActionSearch icon. Despite my efforts, the style does not seem to be applied as expected. Although the Paper component receives styles without any problems, the icon component seems to be resistant t ...

Using Semantic-UI causes issues with the functionality of the height property set to 100%

View my CodePen here <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.min.css"> </head> <body> <div class="ui secondary pointing menu" id=" ...

Changing the custom route in React Router to utilize a render prop instead of the component prop

I'm currently working on a React app that incorporates React Router. I've been encountering a bug in my code stemming from my custom ProtectedRoute component: const ProtectedRoute = ({ component, ...args }) => ( <Route component={with ...

Next.js encountered an error: React cannot render objects as valid children, as it found a promise object instead

Currently implementing dynamic routing in Nextjs and aiming to obtain the client's IP address before initiating Server Side Rendering. This is crucial for adding custom headers required for Social Media sharing. Below is the code snippet: function m ...