Enhancing Class Components with a different approach to styling rather than using makeStyles

I have come across examples that demonstrate how to utilize the makeStyles hook in order to style functional components in Material Design. However, since I am working with a class component, I am unable to use hooks. The code snippet often used for functional components is shown below:

    const useStyles = makeStyles((theme) => ({
    margin: {
    margin: theme.spacing(1),
    },
    }));

When it comes to styling elements within the return() section, they typically do something like this:

className={classes.margin}

How can I achieve similar functionality but for a class component?

Answer №1

When working with class components, you can utilize the withStyles wrapper to apply styles.

import React, { Component } from "react";
import { withStyles } from "@material-ui/core/styles";

class CustomComponent extends Component {
    render() {
        const { classes } = this.props;
        return <div className={classes.styledLine}>Styling using withStyles</div>;
    }
}

const useStyles = (theme) => ({
    styledLine: {
        color: "red"
    }
});

export default withStyles(useStyles)(CustomComponent);

Check out a demo of this implementation here:-
https://codesandbox.io/s/smoosh-morning-tjh7y?fontsize=14&hidenavigation=1&theme=dark

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

Adjust the width of the MUI Drawer on the right side

I'm currently working with the Material UI Drawer component and I would like to add a resizable feature to it. I managed to achieve this by using a CodeSandbox example, which worked perfectly. The only issue is that in the CodeSandbox, the Drawer is p ...

Caution: An object with a "key" prop is being spread in JSX within Material UI Autocomplete

Having an issue with the Material UI Autocomplete component in Next.js. I kept getting a warning related to the renderInput function, specifically about the lack of a "key" property in "...params". I tried to resolve it but couldn't figure out how to ...

I can't seem to get React Fetch and the expressJS post method to cooperate - what could I be overlooking?

I have been diving into the world of React and recently ventured into working with expressJS. However, I encountered an issue with the POST method as the data is not being successfully posted to the server. While I believe my fetching of the post method is ...

Building a dropdown feature for rows in a table using ReactJS

I am utilizing a Material UI Table, and within one of the columns I have a SelectField component that displays a dropdown with a few selectable items. Here is a snippet of the code: <TableBody displayRowCheckbox={this.state.showCheckboxes} ...

Troubleshooting NextJS useEffect with localStorage

I am attempting to retrieve a string from local storage, perform a check against it, and redirect the page based on the value stored in local storage. However, I am facing an issue where the page is briefly visible before redirecting to the original page. ...

The copying process of the React App Docker image seems to be dragging on for quite

My Dockerfile is shown below: FROM node:16 as build-stage WORKDIR /app COPY package*.json /app/ ARG PROJECT_NAME=react-ui RUN npm install --force COPY ./ /app/ RUN npm run build FROM nginx:alpine WORKDIR /usr/share/nginx/html RUN rm -rf ./* COPY - ...

Struggling to implement Yup Validation in MUI while using React Hook Form in a masked Controller field

I've hit a wall with React Hook Form phone masking and validation. Any assistance would be greatly appreciated! My setup involves using React Hook Form with Yup for form validation and MUI for UI. I managed to implement masking on my phone field usin ...

Issue encountered while deploying Next.js application on vercel using the replaceAll function

Encountering an error during deployment of a next.js app to Vercel, although local builds are functioning normally. The issue seems to be related to the [replaceAll][1] function The error message received is as follows: Error occurred prerendering page &q ...

I am having trouble scrolling through the main content when the side-drawer is open. How can I fix this issue?

When the sidebar is opened, I am facing issues with the main content scroll and certain fields such as select options and search bar not functioning properly. I have included the main content in the routes from which it is being loaded. However, the scroll ...

What are the best practices for integrating breakpoints in MUI?

I'm looking to make my project responsive by incorporating MUI breakpoints, but I'm struggling to grasp how to implement them in a straightforward manner. The concept of creating a const styles as outlined in their documentation seems a bit compl ...

Adjust the stroke and fill colors of an SVG element when hovering over it

I am facing a challenge with an SVG image that I have: https://i.stack.imgur.com/r4XaX.png When hovered over or clicked, it should change to https://i.stack.imgur.com/EHRG2.png Current Icon <svg width="24" height="24" viewBox="0 0 24 24" fill="non ...

Cyrillic characters cannot be shown on vertices within Reagraph

I am currently developing a React application that involves displaying data on a graph. However, I have encountered an issue where Russian characters are not being displayed correctly on the nodes. I attempted to solve this by linking fonts using labelFont ...

MUI - The helper text for a Select using a TextField is appearing below the input field rather than within the input field itself

This is how the content will be displayed. https://i.stack.imgur.com/fwvcW.png Displayed below is the code for rendering: <Grid container spacing={2}> <Grid item xs={space} key={1}> <TextField value={""} onChang ...

A step-by-step guide on processing payments via Stripe using a custom payment form designed with Material UI in a React application

In my e-commerce site, I am using the Material UI checkout template that can be found at this link. Users have to enter payment details in Step 2 and review the Cart List, Shipping Address, and Contact & Payment Details in Step 3. At Step 3, when the user ...

Why doesn't setting the SVG viewBox to "0 0 100 100" scale my path to 100%?

My current dilemma involves an SVG path that is only displaying at 50% of its intended size. How can I adjust it to show at 100%? Below is the code snippet in question: <div className="svgPathContainer"> <svg width="100%" he ...

Several conditional statements in JSX

In my JSX Return() code, I am encountering an "If" issue when one of my conditions has 2 different 'onClick' events. There are 2 'a' tags, where one will display button 'X' if a statement is true and the other will display but ...

How do I adjust the margin on MuiButton's endIcon in Material UI?

Just starting out with Material UI, I tried to create a navigation component by following the documentation. While working on it, I encountered an issue with Buttons with icons and label. When I created my button component, I noticed a significant gap betw ...

How to horizontally align Material-UI elements in ReactJS

My goal is to create a user-friendly form where respondents can input answers to questions and select one as the preferred option by using a radio button. However, I'm facing an issue where Material-UI displays each element on its own line. This is t ...

Is it possible to nest slices within slices? What is the best way to distribute shared state logic among slices?

EDIT: I am currently attempting to implement this functionality using Redux Access the codesandbox here. For a quick visual reference, visit: https://jsfiddle.net/59r31Lmt/ I am aiming to create a system where clicking on a camo square activates it. Th ...

How to efficiently fetch Redux state through reducers with an action-based approach

Utilizing Redux actions to manage a list of contacts, I am facing an issue where I am unable to retrieve the actual state contact. Despite setting the contact in the action, all I receive is the contact set within the action itself. Can someone guide me on ...