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

Images displayed alongside webpage contents

I'm having trouble getting these photos to display in the same row. Any suggestions on what I should change? https://i.stack.imgur.com/EbvmR.png I've attempted using float left and other commands, but one picture continues to stay in the lower r ...

Encountering difficulties in updating CSS styles using the useState hook in React

I am currently working on creating a modal in react that changes the background color when opened. The goal is to have the background color darken when the modal is activated and return to normal when the modal is closed. I attempted to achieve this using ...

Tips on customizing the appearance of JavaScript output?

I recently created a plugin for my website with JavaScript, and one of the lines of code I used was output.innerHTML = "Test"; Is it possible to apply CSS styles to this element, or is there an alternative method? ...

The Link tag in Next.js may have issues in production, but functions properly when tested locally

nex config babelrc home page Having issues with my link tag despite trying various solutions. I am utilizing Next Js with Ant design While running locally, the "Go-to Shop" button link functions properly (redirecting to the shop page). However, ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

Price Adjuster Tracker

Recently, I coded a small JavaScript program with the purpose of: incrementing or decrementing the quantity of an item by 1 adjusting the price of an item based on the initial price However, my excitement turned to disappointment when I encountered these ...

Terminate the bash script if there is a failure during the React build process

I am facing an issue with some deployments that are breaking production due to the bash script continuing even if the build fails. Is there a way to ensure that the script stops running if npm run build fails? #!/usr/bin/env bash source .env ENVIRONMENT= ...

Positioning two elements next to each other and keeping them aligned evenly

CSS - How to Float Two Elements Side by Side I am facing a similar challenge in achieving the layout I want. With a percentage-based layout, either my menu overlaps with the content or the content gets pushed below the menu when viewed on mobile devices. ...

How to troubleshoot Javascript code that fails to identify if a color is white

What could be causing my code to malfunction? I am attempting to determine if the paragraph text color is white (as determined by CSS settings). If it is indeed white, I want to change it to black for better visibility. function adjustColor() { if (d ...

Can a horizontal navigation bar be designed to span the full width of the page without relying on a table?

I'm trying to create a horizontal navigation menu with variable width buttons that spans the full width of the containing div. I was successful in achieving this by using a table, as demonstrated in this example. The table cells adjust their size base ...

Unfortunately, I am unable to utilize WebStorm in combination with React

Struggling to set up a React project in WebStorm, it's not running and ESLint isn't recognizing it. Started a new React project with Node.js v16: Successfully created the React project: Encountering issues as ESLint is not recognizing the ...

The div containers are unable to be positioned side by side

Hey there, I'm having trouble with my coding right now. (I'm not sure if you can see it, but the code is creating a div - frustrating). Here's an image of the code: No matter what code I try to use to align the content center, left, or righ ...

When refreshing the React page, it appears blank

Encountering a issue with one of the pages on my react website. Whenever I attempt to reload the Home.js page by refreshing the browser, it displays blank. However, when using the back navigation button in the browser, it functions correctly. I've che ...

Customizing default breakpoints in MUI System using typescript

I have been attempting to establish new breakpoints for my container width using the MUI System. While it functions properly without TypeScript, my custom breakpoints are not being recognized in my TypeScript project; only the default breakpoints (xs, sm, ...

Switching the API endpoint based on the environment (React/Django)

In the process of developing a React app within a Django project and linking them via the Django rest framework, my approach involves utilizing axios for API calls. During development, I call a localhost URL to access the API by setting up an axios instanc ...

Why are boolean values used for breakpoint props (xs, sm, md...) in material-ui components?

I have been struggling to understand the purpose of using booleans as values for breakpoint props in the grid component of material-ui. Despite consulting the grid API documentation, I couldn't find much information or results from experimenting with ...

What could be causing the lack of value appearing after I clicked the button?

Setting the value for the array of images as an empty string does not return the expected result. When I move one of the 4 images and click the button, I anticipate a new array with the information of one of the four images including src, x, and y coordina ...

Using jQuery to modify the border of a particular row

I am facing an issue where I need to apply a border to two specific rows in a table after it has loaded. These two rows are consistent every time the table is loaded. The code snippet below works, but the default line breaks between the table rows prevent ...

The issue of a background image not appearing on Chrome and Firefox has been identified

I am experiencing an issue where a background image is not showing up on Chrome or Firefox, but it displays properly on other browsers. The problem occurs with relative and hard links Substituting the image file works, but the video disappears Adblock is ...

Executing the onChange event and retrieving the outcome in JestJS

In one of my classes, I have an input that triggers onChange event to update certain properties based on user input. My goal is to set a value for this input, trigger the onChange method, and then retrieve the updated result from one of the properties. Bel ...