Utilizing the `theme` property in makeStyles with Material-UI

I am currently working on passing down an muiTheme to a component through the use of ThemeProvider, and I also want to apply it to the children components. Furthermore, I aim to utilize the theme's properties in both components by creating a classes object using makeStyles.

The specific components/files involved are as follows:

  • Component LeftSection | renders Subsection
  • muiTheme LefSectionTheme | used for classes objects in LeftSection

  • Component RightSection | renders Subsection

  • muiTheme RightSectionTheme

  • Component Subsection

My goal is to include a classes object created with makeStyles() in each component, leveraging the theme's properties. I haven't included any code snippets here as I've been experimenting with various functions and configurations, but I suspect there may be gaps in my understanding of how some of these functions operate.

To visualize this scenario without classes, you can view this reproduction: LeftSection and RightSection rendering Subsection with their themes - my objective is to incorporate classes into this setup.

Below is the code snippet for the Subsection component where I intend to utilize classes:

import React from "react";
import { useTheme } from "@material-ui/styles";

function Subsection(props) {
  const theme = useTheme();
  return (
    <p
      style={{
        color: theme.palette.primary.main
      }}
    >
      test
    </p>
  );
}

export default Subsection;

Any guidance on how to achieve this would be greatly appreciated.

Answer №1

Here is an example of how classes can be used to apply styling based on a specific theme:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  paragraph: {
    color: theme.palette.primary.main
  }
}));
function Subsection(props) {
  const classes = useStyles();
  return <p className={classes.paragraph}>test</p>;
}

export default Subsection;

https://codesandbox.io/s/serene-pond-t07i1?fontsize=14&hidenavigation=1&theme=dark

It's important to provide context when seeking help so others can better understand your question and provide more tailored assistance.

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

Is there a way to locate ComputedStyle elements using Nokogiri?

I am currently using Ruby along with Nokogiri to parse through HTML documents. I am looking to select all nodes that match a CSS class with the style attribute display: none, but the CSS class is unknown in advance. For example: <html> <body&g ...

Pause before proceeding to the next iteration in the loop

I am currently facing an issue while trying to send a message through an API using a function. The function returns a value called messageLogId, which I'm attempting to update in the main loop at Attendence. However, when executing the code, the value ...

I'm having trouble setting the margin to 0 in Bootstrap 4. I'm only using CSS for styling and JavaScript for

Currently in the process of setting up a grid layout for a webpage under development. Encountering an issue with the .container class, that is supposed to contain all my div elements, not having any margin. Managed to remove the margin on the left side usi ...

The current state of my DOM doesn't match the updated value in my model. The update is triggered by a function that is called when a button is clicked

app.controller('thumbnailController', function ($scope) { debugger $scope.customers = [ { name: 'Ave Jones', city: 'Phoenix' }, { name: 'Amie Riley', city: 'Phoenix&ap ...

AJAX response for form validation

I need to validate my contact form when the submit button is clicked. If all fields are valid, I want to display a Processing message using AJAX, followed by a success message with the entered name. The content of my Form is: <form onsubmit="return va ...

Adding an authentication header in redux-api-middleware: steps and best practices

Procedure import { CALL_API } from 'redux-api-middleware'; export const MOVIES_GET_SUCCESS = 'MOVIES_GET_SUCCESS'; export const fetchMovies = () => { return { [CALL_API]: { endpoint: 'http://localhost:3005/api/mov ...

Tips for showing items on the screen with a search text input in Expo and React Native

Utilizing react native and expo, I have JSON data presented on the iOS simulator screen that was fetched from an API. Positioned at the top is a search bar where users can query the displayed data. For instance, if the data appears as follows: A a compa ...

Suggestions for altering the CSS styles of my Swiper slides (currently utilizing v10.3.1)

Currently, I am utilizing the Swiper plugin on my website built with React and Next.js. However, I am looking to customize the bullets and buttons of the slider. I have attempted following some video tutorials without much success. Is there anyone who ca ...

Could the jQuery not be running due to the .htaccess file? How can I resolve this issue?

Good Day, I am encountering a persistent pop-up issue with my Joomla! template, displaying only the word "here" in the browser as shown in this Screenshot After investigating, it was suggested that the .htaccess file may be responsible for this behavior. ...

It is not possible to change the maximum height of a Popover in Material UI

I am currently working with the Popover component in material-ui, but I am facing an issue modifying the max-height property that is calculated as max-height: calc(100% - var). I have attempted different solutions such as using className and override, but ...

How to implement div scrolling on button click using Angular 4

Is there a way to make the contents of a div scroll to the left when one button is clicked and to the right when another button is clicked? I've tried using ScrollLeft, but it doesn't seem to be working... Here's the HTML code: <button ...

Combining jQuery and CSS for seamless background image and z-index chaining

I am facing an issue where I need to add a background image within a div containing a generated image with the class .result. Despite my attempts, I have not been successful in displaying the background image correctly. The code snippet I used is as follow ...

What methods should I use to modify the upcoming array of objects?

I have been struggling with this exercise for about an hour now, and I can't figure it out. Can someone please help me with this? Here is the array that I retrieved from the database: View Base Array Image let data = [ { "name": "October : 2019", "u ...

Is it possible to use Firebase auth.user in order to retrieve the signed-in user directly?

As I develop a webapp with NextJS v13.4 and firebase as my backend using the firebase web modular api, I came across a statement in the documentation: "The recommended way to get the current user is by setting an observer on the Auth object." ...

Navigating HTML with Node.js and Express.js

I have been working on routing multiple HTML pages in my project. The index.html file loads without any issues, but when I try to load raw.html, an error message pops up: Error: Failed to lookup view "error" in views directory Below is part of my app.j ...

The React application ceases to function properly as soon as I introduce a textfield

As a newcomer to React, I am facing an issue while working on the front-end UI of a web application. Whenever I try to add a text field to the box I created, the app fails to render anything other than a blank color on the screen. Here is how it looks: W ...

Exploring elements using dynamic xpaths in selenium

When working with Selenium WebDriver, I am facing difficulties selecting the <input class="cs-autocomplete-input" tabindex=""> element due to its fluid ID. This is because there are multiple items with the same class name on the web page, making it c ...

The material UI date pickers are lacking Arabic translations

Upon reviewing the contents of this folder: https://github.com/mui/mui-x/tree/next/packages/x-date-pickers/src/locales, I noticed that there is no arabic locale available. Does the x-date-pickers repository lack support for arabic language? I remember it ...

Error: The Material UI Range Min Max component is throwing an error because it cannot access the property 'focus' of a null value

Recently, I started exploring React and MaterialUI. I am currently attempting to create a range filter featuring minimum and maximum inputs, as well as a range line using MaterialUI along with an example involving number-format. The issue arises when I ut ...

The Gatsby Image using Netlify CMSField should not include a selection as the type "String" does not have any subfields

Today, I encountered a puzzling issue that has left me stumped. As someone relatively new to React and Gatsby, I've been working on a Website powered by Netlify CMS. Everything was running smoothly in the past few days until today when something stran ...