The Material UI - makeStyles function is having difficulties generating the intended styles

After implementing withStyles and makeStyles from material-UI, I encountered an issue where the CSS styling was not as expected, despite the correct class name being returned.

For example, consider the following component:

import React, { Component } from 'react'
import { Container, CssBaseline, Grid } from '@material-ui/core';
import { makeStyles, withStyles } from '@material-ui/styles';

const styles = makeStyles(theme => ({
    letMeCheck: {
        backgroundColor: 'red'
    }
}));

class App extends Component {
    render() {
        const { classes } = this.props;

        return (
            <Container maxWidth="lg">
                <CssBaseline />
                <Grid container>
                    <Grid className={classes.letMeCheck} item xs={12} sm={6}>
                        A
                    </Grid>
                    <Grid item xs={12} sm={6}>
                        B
                    </Grid>
                </Grid>
            </Container>
        );
    }
}

export default withStyles(styles)(App);

(Observing in Chrome's inspector) The grid div has the class "App-letMeCheck-2" however, the corresponding style applied is:

.App-letMeCheck-2 {
    0: m;
    1: a;
    2: k;
    3: e;
    4: S;
    5: t;
    6: y;
    7: l;
    8: e;
    9: s;
    10: -;
    11: l;
    12: e;
    13: t;
    14: M;
    15: e;
    16: C;
    17: h;
    18: e;
    19: c;
    20: k;
    21: -;
    22: 1;
}

Answer №1

makeStyles function will return a hook, usually referred to as useStyles. On the other hand, withStyles is considered a higher-order component (HOC) and they are not compatible with each other. Both methods serve the same purpose but take different approaches.

// Implementation using makeStyles
const useStyles = makeStyles(styles)

const Component = () =>{
    const classes = useStyles()
}

// Implementation using withStyles HOC
const Component = withStyles(styles)(({ classes }) =>{

})

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

Tips on positioning the image to the left of your content instead of above it

Currently, I am following a tutorial on Flask and attempting to include the profile picture in each article displayed on the website. Here is the code snippet used to display an article: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

Tips for aligning text to the right and button to the left while ensuring responsive design with CSS

Trying to align text on the left and the button on the right with space between them in a responsive card design. The button has a background image added to it. Here is my attempted CSS, but the design is not yet fully responsive: .component { disp ...

Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial onlin ...

Integrating concealed elements into jspdf

I am currently working on incorporating a hidden div into my jspdf file. Utilizing html2canvas for this purpose, I find it necessary to briefly make the div visible, add it to the pdf, and then hide it again. This method is not ideal as the content moment ...

Stop Antd Modal from automatically scrolling to the top

At the moment, I'm utilizing Ant Design (v4.22.8) and Next.js (v12.3.4) in my project. One issue I've encountered is with a Modal component that activates when a button is clicked. Instead of overlaying the current content on the screen, the moda ...

Passport.js does not provide authentication for server-side asynchronous requests

Recently, I configured Passport.js with the local-strategy on my express server. Interestingly, when I am logged in and send an asynchronous request within NextJS's getInitialProps, it allows the GET request through client-side rendering but not serv ...

What is the best way to add a textfield within an image?

Does anyone have a code snippet that can generate a "search box" like the one on www.dx.com? I was shocked to find that searching for "textfield inside image" only yielded results for "image inside textfield". ...

Tips for managing data received from an Axios post request

I have a setup with a react frontend and node.js backend. Currently, I am successfully sending a POST request using axios: const [logins, setLogins] = useState({}); function updateLogins(e) { setLogins({...logins, [e.target.name]:e.target.value} ...

Performing an action does not alter the current state

My goal is to include user metadata within my store when the screen is mounted. However, despite sending the action to the reducer, the store remains unaltered. Upon sending the action, I anticipate the props to appear as shown below: {addUserMetaData: ...

Error message occurs when MUI components return Uncaught TypeError

Hi there, I would really appreciate some help. I'm currently working on a react project using MUI and encountering an issue where certain components are not functioning correctly, resulting in an Uncaught TypeError error message. Could someone assist ...

Collaborating and utilizing React Components in multiple projects

Seeking advice on a conceptual dilemma. I am managing two projects that are built on separate react services, and there are many components that could be shared between them. How can I create a dynamic shared-component project that can be accessed simult ...

Troubleshooting MaterialUI: Is there a way to prevent the InputLabel text from disappearing when setting a background color on the Select component?

My goal is to change the background color on dropdown elements while keeping the default text visible. Currently, if I don't explicitly set a background color on the Select component, the text specified in the InputLabel displays correctly. However, o ...

What is the method to assign an initial value to React.createRef()?

Scrolling management in my component is handled through a ref that represents the current scroll value. I've chosen to use refs instead of state and setState because using setState while the user is scrolling would result in a choppy and unresponsive ...

React maintains the state of just a single element within an array at a time

I'm faced with a roadblock while developing my covid19 application. The app should display a list of countries on the left side of the screen, allowing users to add any number of countries to the right side for more detailed covid data. I'm still ...

Retrieving information from Material UI's DataGrid

I am currently working on tables using Material UI. Within the table, there is data that I need to pass to the next component. I attempted to use tableRef = useRef(), but unfortunately, it returned as undefined. Is there a different approach that could b ...

Encountered an issue while trying to render the Material UI autocomplete component

I am currently working with react v17.0.2,react-redux v7.2.6, @reduxjs/toolkit v1.7.2 While trying to render a page in React, I encountered an error related to the Autocomplete component of Material-UI. The issue was further complicated by my use of TypeS ...

Implementing next-i18next in Components

I followed all the instructions in the documentation without success. Why isn't the code below functioning properly? With NextJs, it seems that Server-Side Rendering (SSR) is only supported for pages in the pages folder. For instance, how can I imple ...

Creating a dynamic overlapping image effect with CSS and JavaScript

My fullwidth div has an image that overlaps it. When the browser is resized, more of the image is either shown or hidden without resizing the actual image itself. I managed to get this effect for the width, but how can I achieve the same result for the hei ...

Unable to access the URL slug within the client component in Next.js version 13

In my upcoming project with Next 13, I have a client-side component that is being rendered under the route /journal/[date] Unfortunately, I'm facing an issue trying to extract the date from the URL. I attempted to use: import { useRouter } from &apos ...

css side by side with immovable element

Seeking assistance with CSS. I am looking to create a layout as follows: I want a center-fixed menu with a width of 960px - this part is straightforward. Alongside the menu, I aim to have two divs: one spanning from the left edge of the screen to the cl ...