"The useTheme hook is not functioning as expected when used within a className

I've defined a custom theme in my App.js file like this:

let theme = createTheme({
  palette: {
    bgCells: {
      textAlign: 'right',
      color: '#8ccadf',
      backgroundColor: '#eef7ff'
    }
  }
});

In another component called Report.js, I want to use the bgCells style from the theme using the useTheme hook, as shown below:

import React from 'react';
import { TableCell, TableRow, useTheme } from '@mui/material';

export default function Report(props) {
  const { info } = props;
  const theme = useTheme();

let myClass;

if (info.is_active) {
    myClass = classes.someStyles;
} else {
    myClass = theme.palette.bgCells;
}

  return (
    <TableRow className={myClass}>
      <TableCell>
        {
          . . . . 

However, myClass = theme.palette.bgCells doesn't seem to be applied to my

<TableRow className={myClass}>

Any suggestions on what might be going wrong?

Answer №2

Using the `palette` parameter in the `createTheme` function is not suitable for styling. It should only contain color information.</p>
<p>The `bgCells` variable is an object and cannot be added as a className. Class names must be strings.</p>
<p>If you want to style the component using an object, you can use the `sx` prop like this:</p>
<pre><code><TableRow sx={{textAlign: 'right', ...}}> ... </TableRow>

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

In React-Redux, attempting to assign a value to an empty string is not permitted

When using the useDispatch hook, I am facing an issue where I cannot set the string to an empty value. Instead, it always sets the value to the last character in the string. App.tsx const dispatch = useDispatch(); dispatch(updateLocation('')); ...

Implementing cookie or local storage functionality in my React.js web application

Currently, I am developing my first react.js project which is a recipe generator. Overview of my project: View 1: Select a base ingredient View 2: Add more ingredients View 3: List of recipes will be displayed based on selected ingredients including th ...

Tips on how to update the status variable to true depending on the index value

Once I click on the close button, the value immediately changes to "Fruit." How can I achieve this? For instance: Apple close Grapes close Pineapples close Alternatively, is there a way to set the state of "cancel" to true ...

Cleaning up React async functions using hooks for fetching data

This code snippet is from a functional component. Within this component, there is a submit() function that handles form submission: async function handleSubmit(event) { event.preventDefault(); try { let resp = await fetch("FOOBAR/BAX", { ...

Continuously replicate SVG horizontally without limit

In search of the perfect visual effect, I am eager to develop an infinite animation featuring a car moving horizontally with various landscape layers passing by in SVG format. Struggling to find a way to seamlessly repeat my SVG landscape layers along the ...

"Navigate with ease using Material-UI's BottomNavigationItem and link

What is the best way to implement UI navigation using a React component? I am currently working with a <BottomNavigationItem /> component that renders as a <button>. How can I modify it to navigate to a specific URL? class FooterNavigation e ...

Generating a React User-Object in JSON Format

Imagine there is a back-end system using Node.js that allows the creation of users with specific attributes as shown below: POST http://localhost:8080/user Authorization: {{adminToken}} Content-Type: application/json { "userID": "test" ...

Moving divs to a separate line in mobile display

I currently have a basic image-thumbnails landing page set up like this: <div style="display: inline-block;"><img style="margin: 3px 3px;" src="..." alt="" width="200" height="200" /></div> <div style="display: inline-block;"><i ...

Effective methods for synchronizing two dropdown menus with varied displays using React

Having a list of countries with specific key-value pairs, I am interested in creating two Dropdown lists. The first will display the keys, while the second will show the corresponding text values. The main objective is to provide the option to select eith ...

Smooth scrolling feature malfunctioning in mobile view

While working on my website, I noticed that the smooth-scroll feature works perfectly on desktop browsers. However, on mobile devices, when I click on a link, it does not scroll to the correct position. It always ends up much lower than expected. Any idea ...

Resetting a selected dropdown value in React Hook Form

Currently, I am in the process of updating the information on the location update form and I want to ensure that the existing values are displayed in the form fields. Below is the code snippet for the update component: import axios from 'axios' ...

Encountering a Vercel deployment failure due to a TypeError: The 'split' property cannot be read from undefined within several objects

I'm having trouble deploying my web application for the first time and encountering this error on Vercel: TypeError: Cannot read property 'split' of undefined at Object.3qS3 (/vercel/path0/.next/serverless/pages/[collection]/[templateId].j ...

Expanding divs automatically depending on the content they contain

Currently, I have been developing a template for a database-driven page that is supposed to contain four divs for content arranged like this: ----- ----- | 1 | | 2 | ----- ----- ----- ----- | 3 | | 4 | ----- ----- The information in each box will always ...

Resizing the Logo as You Scroll

I need my logo to shrink when I activate the slideshow, similar to what is shown in the image. https://i.sstatic.net/WhobD.jpg However, I'm struggling to get the jQuery code to work. Can someone please demonstrate how I can shrink a logo, like the o ...

I've encountered an issue with react-responsive-carousel not functioning properly with server-side rendering (SSR) in NextJs. Can anyone provide an example of how to enable SSR for this component

I'm currently working on a project involving e-commerce and need to retrieve product details data via SSR to display it on the screen. Everything is showing up correctly except for the product images. I'm using the react-responsive-carousel compo ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...

Center two elements within a div container both horizontally and vertically

I am trying to vertically center a text and a button within a container. The text should be positioned in the middle of the div, while the button should be aligned to the right. What is the best way to achieve this layout where the text element is centere ...

Randomly positioning an image while the page is loading

I've been developing a portfolio website to showcase my design work, but I've encountered a minor issue. My goal is to have the images load in random positions and then make them draggable using Dragabilly. However, sometimes all the images end ...

What could be causing npm install to consistently return error code 1?

Whenever I run the command npm install, I keep encountering this error message. Despite downgrading my Node.js to the latest stable version, the issue persists. npm ERR! code 1 npm ERR! git dep preparation failed npm ERR! command C:\Program Files&bsol ...

My draggable item seems stuck in place. Any ideas on how to get it moving again?

I have been trying to implement some code I found on this link. Despite adding all the necessary file links, the code is not functioning as expected. It should be able to move within the bounds of a container, but it's not working properly. var max ...