The button's background color fails to update promptly in React, despite other CSS properties functioning properly

Utilizing React and Material-UI, I've implemented data filtering using filter and map methods. However, I noticed that when I click on a button, all CSS properties update correctly except for the background color, which only updates after clicking outside the area once after the button click.

Below is the snippet of my code:

   const years = [2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020];
   const Cards=()=>{
   const [activeYear, setActiveYear] = useState(null);
...
     // filter button
      {years.map((yr,index)=>(
                    <Grid item xs={12} sm={4} lg={6} style={{padding:'8px'}}>
                        <Chip 
                            key={index}
                            label={yr}
                            onClick={e=> handleYear(e,yr)}
                            className={activeYear==years[index]?classes.activeYear:''}
                        />
                    </Grid>
                    )  
        )}
        ......
       }

You can access the sandbox via this link: https://codesandbox.io/s/filter-using-tag-e65yy?file=/src/Cards.js

https://i.sstatic.net/lxQl1.gif

Answer №1

After following @m4n0's advice, I successfully resolved the issue by incorporating background color into the css file to overwrite the default hover styles.

.MuiGrid-item .makeStyles-activeYear-8.MuiChip-clickable:hover,
.MuiGrid-item .makeStyles-activeYear-8.MuiChip-clickable:focus {
  background: #cfd;
}

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

The persistence of authentication state in Next.js Firebase is inconsistent

I can't figure out why Firebase auth isn't persisting. My project is quite basic at this point, with a simple sign-in function: await signInWithEmailAndPassword(auth, email, password); In my layout.tsx, I have the onAuthStateChange: const unsubs ...

I'm having trouble aligning navlinks inline in React-Bootstrap

I am facing an issue while creating a navbar in my project. The nav links are appearing as points and not inline. Interestingly, when I refer to the example provided in the react-bootstrap documentation, the nav links display inline. Below is the code sni ...

Unexpected behavior encountered when implementing specific Textfield validation with Material UI

Currently running a project on Node and utilizing React for the front-end, I have encountered an issue with setting .env variables. The project is built with Material UI, and most TextFields are working as expected with their specified validation rules. ...

Leveraging python's BeautifulSoup library, one can easily implement HTML selection

Recently, I've started diving into Automate the Boring Stuff and exploring how to create my own programs. Currently, I'm experimenting with beautiful soup's 'select' method in order to extract the value '33' from this cod ...

Utilize CSS to prioritize the image and ensure it is responsive

I'm utilizing the 'featurette' feature in bootstrap. Currently, the page displays text on the left and an image on the right. As you reduce the browser size, they stack on top of each other, with the text appearing above the image. How can I ...

Implementing a service worker into a ReactJS application

I'm currently working on a small single-page ReactJS application that will retrieve data from a third-party API (such as YouTube videos) to display them. I don't plan to have a backend for this project, but I want to make it offline-friendly by u ...

Moving up stacked columns with bootstrap or any other CSS technique

Is there a way to make column [3] move up if column [2] is taller than column [1]? [1][2] [3][4] Check out this bootply example of the layout : http://www.bootply.com/KbAf8UOk9c Currently, there is empty space between column [1] and [3] that I would li ...

React context isn't refreshing the list upon editing

I'm having an issue with my CRUD application that utilizes an array to generate a list. The problem is that when I try to delete an element, it always removes the last one instead of the selected one. However, interestingly enough, the list updates co ...

How can the parent Flexbox in a nested Flexbox shrink and remain at the top, wrapping to the next line only when the available space is insufficient for display?

What is the best way to ensure that box 1, 2, and 3 stay on the right of box A in a desktop browser, but shift below box A if there isn't enough space (e.g. in a smartphone browser)? I am open to solutions using either Bootstrap 4 or 5. https://i.sst ...

The jQuery UI dialog stubbornly refuses to close when tapped, yet opens without hesitation

I'm struggling to understand why my dialog boxes are not closing when I tap outside of them. I've tried using different selectors such as document, window, .ikon_picmap, but nothing seems to be working. What am I missing here? Check out the code ...

Which is the best choice for retrieving a value - utilizing an object with an id or an array with an index?

Imagine having a group of different animals in a zoo. lion: {'type': 'mammal'} tiger: {'type': 'mammal'} ... You can organize them into an array of animals const [animalsArray, setAnimalsArray] = useState([lion, tig ...

Transferring an array of data between two screens within tab navigation using react-navigation in react native applications

My current setup involves using react-navigation without redux. I have two tabs, each with its own stack navigator containing one screen. Both of these screens require an array of locations. Right now, I am initializing and updating the locations array sep ...

Issue with a "hover" effect on a specific input[type="submit"] button

I'm having trouble getting my CSS styles to apply correctly to the input field. Can anyone help me understand why this is happening? If you'd like to take a look at the code, here are the links to the .html file and the .CSS styles. The goal is ...

Utilizing properties to transfer a reference object to a nested component

Is it safe to do the following in React: function Parent() { const myRef = useRef([1, 2, 3]); console.log("parent: " + myRef.current); return <Child myList={myRef.current} />; } function Child({ myList }) { const [currInt, setCurrInt] = useS ...

Ways to confine the mui grid within a container

I recently created a mosaic grid layout using Mui grid My goal is to confine the grid box to a specific area on the screen, similar to this: https://i.stack.imgur.com/5en1i.png Users should need to scroll to view additional content However, the current ...

Is there a way to create a transparent color for the Dropdown Toggle?

As I work on expanding my understanding of HTML and Bootstrap, utilizing Bootstrap version 5.3.0 Alpha 3, I encounter an issue with the Dropdown feature. Typically, when a Dropdown is used within an element such as an icon or text, it displays an arrow sym ...

Is it possible to apply identical css styles to multiple ids at once?

Is there a way to accomplish this? .apple, .orange, .kiwi h1 {color: #892828;} I'm having trouble making it work. Any suggestions? ...

I'm noticing that my Tailwind styles don't take effect until I redefine them. What could be causing

My Tailwind classes are giving me trouble. I faced an issue when I created a new component in the directory and placed my Button component there. Whenever I restart the project in terminal, the styles do not apply to my button unless I manually define th ...

Having trouble getting the React Router Dom Link function to function properly

Within my React component, PostItem, I have implemented a link to the edit post page. Surprisingly, this particular link does not work while all other links in different components are functioning properly. Although the page is rendered successfully with ...

Changing color of entire SVG image: a step-by-step guide

Check out this SVG image I found: https://jsfiddle.net/hey0qvgk/3/ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" width="90" height="9 ...