The correct rendering of React css only occurs after refreshing the IDE

Can anyone assist me with understanding this unexpected behavior in my CSS? I'm struggling to figure out why it's happening and how to correct the CSS imports.

In my react app, there is a single component that imports a stylesheet from a separate file called brand.js. The component includes an

<Avatar className={classes.avatarTM}>
, where brand.js defines the class with a Dark Blue background color.

However, when I view the app in Chrome, the avatar displays as grey instead of blue. Upon inspecting the element, I notice two styles being applied: .makeStyles-avatarTM-11 with the expected backgroundColor of primary.dark, and .MUIAvatar-colorDefault with a backgroundColor of #bdbdbd. Unfortunately, colorDefault takes precedence over avatarTM-11, resulting in a grey avatar, which is not what I want.

Interestingly, if I update the // deleteme comment in brand.js using Visual Studio and save the file, React automatically refreshes the avatar in Chrome displaying the correct blue color. Now, avatarTM-11 is given priority over colorDefault.

However, upon reloading the page in Chrome, it reverts back to the default grey color.

The confusing aspect arises when I create a new file named brand2.js, copy the exact content of brand.js into this new file, and then modify the imports in App2.js and testComponent.js to refer to brand2 instead of brand. Surprisingly, this resolves the issue - now avatarTM-11 consistently takes precedence over the default style, regardless of what loads the page, resulting in a blue avatar.

So, should I just use brand2 instead of brand? Yes. But as soon as I delete brand.js, the problem resurfaces.

I am left puzzled by what exactly is happening here. How can I ensure that my defined avatarTM style always prevails, regardless of external factors such as the loading mechanism or the name of the stylesheet being used? Why does the presence or absence of an unused file impact the avatar's appearance?

app2.js:

import React from 'react';

import { ThemeProvider } from '@material-ui/core/styles';
import { theme } from './components/Brand';

import TestComponent from './components/testComponent';

export default function App() {
  return (
    <React.Fragment>
      <ThemeProvider theme={theme}>
        <TestComponent />
      </ThemeProvider>
    </React.Fragment>
  );
}

testComponent.js:

import React from 'react';

import Avatar from '@material-ui/core/Avatar';
import Card from '@material-ui/core/Card';
import CardHeader from '@material-ui/core/CardHeader';

import { useStyles } from './Brand';

export default function TestComponent() {
    const classes = useStyles();
    return (
        <Card className={classes.card} variant="outlined">
            <CardHeader
                avatar={
                        <Avatar aria-label="testComponent" className={classes.avatarTM}>
                            t
                        </Avatar>
                }
                title="testComponent"
            />
        </Card>
    )}

brand.js:

import { createMuiTheme } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';

const theme = createMuiTheme({
  palette: {
    primary: {
      main: '#014EAA',
      contrastText: '#ffffff',
    },
    secondary: {
      main: '#0099CC',
      contrastText: '#ffffff',
    },
    info: {
      main: '#666666',
      contrastText: '#ffffff',
    },
    error: {
      main: '#FF6600',
      contrastText: '#ffffff',
    },
    success: {
      main: '#339933',
      contrastText: '#ffffff',
    },
  },
});
// deleteme
const useStyles = makeStyles(theme => ({
  root: {
    color: theme.palette.primary,
    fontSize: 10,
    padding: '6px 12px',
    fontFamily: ['sans-serif']
  },
  card: {
    minwidth: 275,
  },
  title: {
    fontSize: 24
  },
  cardTitle: {
    fontSize: 24,
    color: theme.palette.primary.main,
  },
  cardSubtitle: {
    fontSize: 14,
    color: theme.palette.secondary.main
  },
  cardDescription: {
    fontSize: 10,
  },
  avatarTM: {
    backgroundColor: theme.palette.primary.dark
  },
  avatarUnknown: {
    backgroundColor: theme.palette.warning.main
  },
  avatarFixed: {
    backgroundColor: theme.palette.primary.light
  },
}));

export { theme, useStyles }

Answer №1

If you want to apply this style forcefully, you can try using the following property:

customAvatarStyle: {
    background: 'theme.colors.primary.dark !important'
},

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

Discover the exclusive Error 404 dynamic routes available only in the production version of NEXT13! Don

Hey everyone, I'm encountering an issue with NEXT's dynamic routing (Next 13). My folder structure looks like this: - user/ -- [id]/ --- page.js It works fine in dev mode but not in production. What am I trying to do? I've created a "page ...

I aim to utilize JavaScript to capture the constantly changing font color that is randomly generated

Is there a way to extract the font color from a table element? #FF0000 For example, it could be #568000 etc.. Here's my table <table class="mytable"> <th class=vertical bgcolor=#C0C0C0 align=center> <font color=#FF0000 size= ...

Positioning Elements Absolutely in Relation to List Items

I am looking to style a list item by adding background-color, border, and other properties. Below is the CSS and HTML I have tried so far. Additionally, I want to use the div element to position more absolute elements in relation to the li > div. ul { ...

Is it possible to manipulate CSS on a webpage using javascript?

I have incorporated this piece of JavaScript to integrate a chat box onto my website: window.HFCHAT_CONFIG = { EMBED_TOKEN: "XXXXX", ACCESS_TOKEN: "XXXXX", HOST_URL: "https://happyfoxchat.com", ASSETS_URL: "https://XXXXX.cloudfront.ne ...

What is the reason for min-content not being compatible with auto-fill or auto-fit?

My confusion lies in the fact that this code snippet works: .grid { display: grid; grid-template-columns: repeat(4, min-content); } Whereas this one does not: .grid { display: grid; grid-template-columns: repeat(auto-fill, min-content); } I am ...

Encountered error E404 during installation of react packages using npm

Encountering an error while developing a Typescript-based fullstack project. The error message is as follows: npm ERR! code E404 npm ERR! 404 Not Found - GET https://registry.npmjs.org/@mui%2ficon-material - Not found npm ERR! 404 npm ERR! 404 '@mui ...

"Manipulate the contents of a state array by adding or removing items at specific indices in React, alongside other array

I am facing a challenge in removing items from an array that have been added to a state array in React. While I can successfully add new items, the removal process is not working as expected. The current remove function removes all elements, but I only wan ...

Creating an adaptable CSS triangle for an image

Is there a way to create a responsive triangle on an image that resizes properly and stays aligned? I'm currently having issues with it becoming misaligned. How can this be resolved? .image { width: 100%; } .image ...

Creating a unique geometric shape using div elements and adding personalized touches to enhance its appearance

Currently, I am working on a project where I need to create a pointer on top of a div, similar to a tooltip. My initial approach was to try the following: https://stackblitz.com/edit/angular-ivy-fa72tz My goal is to achieve the following design: I aim ...

React-Router failing to properly unmount components when the location parameter changes

I'm struggling with a frustrating issue - I have a simple app created using create-react-app and I'm utilizing React-Router. JSX: <Router> <Route path="/pages/:pageId" component={Page} /> </Router> Within the Page ...

What is the best way to pass a variable and its corresponding state-updating function as a prop in a React component?

Considering I have a variable defined as follows: const [APIKey, setAPIKey] = useState([]) Is it possible to group these together into a single variable and then pass it as a prop? const passAPI = {APIKey, setAPIKey} Here is my approach to passing it alo ...

Unable to achieve full width for a div within a scrollable container

Currently, I am in the process of constructing a new website using Gatsby.js. However, I have encountered a minor issue with some CSS related to code blocks. Since CSS is not my strong suit, I have been unable to resolve it thus far. Below is a runnable sn ...

You can move freely between classes A and B, as well as classes A and C, but there is no transition allowed between

A Brief Overview... Take a look at the HTML structure below: <div class="wrapper"> <div class="item above"></div> <div class="item active"></div> <div class="item below"></div> <div class="item ...

Troubleshooting: Else block not functioning as expected within a React JS map function

I have a notification feature that makes an API call every 10 seconds to display an alert based on the response. However, I'm encountering an issue where the div is not being rendered properly. The div should be displayed based on certain conditions w ...

Is it possible to make a div element "wrap" using CSS techniques?

(Look at the final solution below) I have a puzzle in my mind, and although I think it might be impossible, I trust the brilliant minds on Stackoverflow to help me out. I have container-div elements that contain one or more child-div elements. These conta ...

How can the scrolling speed of a ScrollView be adjusted?

Recently, I've been experimenting with a Scrollview that contains a list view with page indicators shown as numbers. My goal is to have the selected number displayed in the center of the view when clicked on. For example: 4 5 6 [7] 8 9 In this sc ...

Tips for arranging cards responsively in a vertical column

I am working on creating simple card items using HTML and CSS. I have successfully aligned the cards in a row on a PC, but I would like them to be displayed vertically in a column on smaller screens. Below is the code I am using. body { width: 100vw ...

How can we make Internet Explorer 11 mimic Internet Explorer 9 in HTML?

How can I ensure that Internet Explorer 11 uses CSS styles from IE9 in my HTML? I have tried the following code: <script runat="server"> private void Page_PreRender(object sender, EventArgs e) { var meta = new HtmlMeta() ...

Is there a way to close the menu in react by clicking anywhere on the

Presently, I have a solution to close my topbar menu that involves clicking the menu icon. However, this method is not satisfactory because it only closes the menu when the icon is clicked. I am looking for a way to make the menu close when any area of th ...

Utilizing MUI Autocomplete with axios request to choose or insert a new option [view example on codesandbox]

I am currently working on implementing an Autocomplete input for categories based on data received from an API. Users should also have the option to create a new category if they can't find a match. Challenges: 1- How can I prevent duplicate keys w ...