Utilizing React Classes Based on Conditions

I'm attempting to add the class active if commentBadgeActive is true. The Scholar image should transition from grayscale to colored.

      <Paper className={classes.paper}>
            <div className={classes.profile}>
                <div className="img-collection">
                    <Tooltip title="Receive 20 likes" arrow>
                        <div className="achievement">
                            <img className={`${likeBadgeActive ? "active" : ""}`} src={Fire} alt="monkey" />
                            <h5>Noble</h5>
                        </div>
                    </Tooltip>
                    <Tooltip title="Receive 10 comments" arrow>
                        <div className="achievement">
                            <img className={`${commentBadgeActive ? "active" : ""}`} src={Scholar} alt="monkey" />
                            <h5>Scholar</h5>
                        </div>
                    </Tooltip>
                    <Tooltip title="Upload 10 memes" arrow>
                        <div className="achievement">
                            <img className={`${screamCountActive ? "active" : ""}`} src={Knight} alt="monkey" />
                            <h5>Contributor</h5>
                        </div>
                    </Tooltip>
                </div>
            </div>
        </Paper>

Customization

 "& .img-collection": {
        display: "flex",
        justifyContent: "center",


        "& .achievement": {
            display: "flex",
            justifyContent: "center",
            flexDirection: "column",
            alignItems: "center",


            "& img": {
                height: 70,
                filter: 'grayscale(100%)'
            },

            "& active": {
                height: 70,
                filter: 'grayscale(0%) !important'
            },

        },
    },

Debugging commentBadgeActive

  const {
    classes,
    totalXP,
    userLevel,
    screamCountActive,
    commentBadgeActive,
    likeBadgeActive,
    profile: { handle, createdAt, imageUrl, bio, website, location, nativeLanguage, learnLanguage }
} = props;

console.log(commentBadgeActive);

The console log returns true, however, the active class does not get applied to the image

Any assistance would be greatly appreciated

Answer №1

You have overlooked adding . before the active class in your code:

"& .img-collection": {
        display: "flex",
        justifyContent: "center",


        "& .achievement": {
            display: "flex",
            justifyContent: "center",
            flexDirection: "column",
            alignItems: "center",


            "& img": {
                height: 70,
                filter: 'grayscale(100%)'
            },

            "& .active": {
                height: 70,
                filter: 'grayscale(0%) !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

Exploring the Integration of Node Modules with React

I am still learning about Node and I'm struggling to integrate an NPM package with my React project running on Node. Currently, I have a React component that successfully uploads a zip file through Node server scripts. Now, I am working on the next s ...

Add to and subsequently remove the possibility of deleting that element

I encountered an issue while moving elements from one div (x) to another div (y). The problem is that the elements in div x are deletable, but once they are copied to div y, they cannot be deleted. Here is the initial state: https://i.sstatic.net/4yQ6U.pn ...

Navigating in Electron with Create React App

I recently completed a project using create-react-app and electron. Everything was running smoothly in the development environment. Check out this image of my dev environment However, once I built the project and attempted to run it with electron, all I ...

Is Immutable state considered a key functional aspect in the ReactJs framework?

One key aspect of an imperative program is the emphasis on state and its modifications. When it comes to ReactJs, there is a push for more functional programming styles, such as using purity and higher-order functions. I'm curious to explore whether ...

Error occurs after upgrading React app to vite due to node-fetch issue

I'm a bit perplexed by this issue. Transitioning the build tool to vite has been seamless except for encountering this error: No matching export in "node_modules/node-fetch/lib/index.mjs" for import "RequestInit" No matching expor ...

When I click on the md-select element, a superfluous "overflow-y: scroll;" gets added to the <body> of my webpage

Typically, I have the following styles on my body: element.style { -webkit-app-region: drag; } However, when I interact with a md-select element (you can observe this behavior on the provided link), additional styles are applied. element.style { -w ...

Ways to slow down page transition on NextJs

I'm currently working on securing my private pages using a HOC withAuth. While the protection is functioning correctly, I am looking to avoid users seeing a loading screen for a split second while the access token is being retrieved from local storage ...

Struggling to publish my Next.js app on Vercel. Any suggestions on how to proceed?

I have been facing an issue with my Next.js project. It runs smoothly on localhost, but I encountered a problem when trying to deploy it on Vercel. Below are the errors that I received: npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! ...

Is it possible to replace Material UI theme with a custom JSS theme?

Currently, I am utilizing JSS themes for styling my components. However, it appears that my personalized JSS theme is conflicting with the JSS theme applied to the Material UI components. The version of material-ui being used is 1.0.0-beta.22. The setup r ...

How can I prevent right-clicking with Ctrl+LeftMouseClick in Firefox on MacOS?

I'm looking to implement a shortcut using Ctrl+LeftMouseClick in my React project. It functions perfectly on Chrome on my Mac, but in Firefox the shortcut initiates a right mouse click (event.button = 2). I believe this may be due to MacOS's Rig ...

Enhance ant design modal functionality by enabling resizing capabilities

I'm experiencing an issue with the Ant Design Modal component as it does not support resizing the modal window. I'm looking for a way to enable manual resizing of the modal window once it has been opened. Is there a solution available for this fu ...

Ensuring Equal Padding on Both Sides of a Div by Setting its Width

In search of a div that houses multiple inner divs with equal padding on the left and right edges. This is crucial for maintaining a fluid layout where the distance between the inner divs and outer div border remains consistent even when the window size ch ...

Is it possible for me to invoke a div within a different component?

I am facing a challenge with a large div component. <div id='download> ..... </div> My goal is to incorporate this same component into a Paper within a Modal. <Modal> <Box sx={style} > <Paper elevation ...

The set method of useState is not causing the specific component to render

My concern arises when trying to update the TaskDetail component located at Right Side. Despite changing the value of the card within the handleCardClick callback, the specific component does not reflect this change. In the provided gif, it can be observed ...

highlight the selected option in the ng-repeat list of items

Looking for some assistance with a coding problem I'm having. I keep running into an error when trying to make a selected item in an ng-repeat list highlight. The CSS style is only applied on the second click, not the first. Additionally, I need to en ...

What are the steps to incorporate swipe functionality into my component?

I've created a carousel using React-slideshow-image, but the issue is that it doesn't support swiping on mobile devices. I would like to implement swipe functionality myself, but I'm not sure how to go about it. Can anyone provide guidance ...

Contrast between pre-tag in HTML and white-space: pre newline exclusion

I originally thought that the pre(html tag) would act just like 'white-space:pre'. However, it turns out that it does not behave in the same way. <pre> aaa bbb </pre> <p style="white-space:pre;"> aaa bbb </p> The <pr ...

Incorrect measurement of text size

My attempt at creating a basic font size changer was working perfectly until I integrated it with the bootstrap framework. Strangely, when I try to increase the font size, it actually decreases instead. var baseFontSize; (function () { "use strict"; ...

"Encountering a TypeScript error when using React Query's useInfiniteQuery

I am currently utilizing the pokeApi in combination with axios to retrieve data import axios from 'axios' export const fetchPokemonData = async ({ pageParam = "https://pokeapi.co/api/v2/pokemon?offset=0&limit=20" }) => { try ...

The process of React life cycle in functional components

I am new to react.js and struggling to grasp class components. The React lifecycle is something I am working hard to understand. ...