What is the best way to toggle DOM classes in React using Material-UI components?

Currently utilizing Material UI alongside React, I have a div element:

<div className={classes.div}></div>

I am attempting to dynamically add a conditional class to it:

<div className={classes.div + divActive ? `${classes.div}__active` : ''}></div>

Unfortunately, the second example is not functioning as expected - it consistently displays

<div class="makeStyles-div-2__active"></div>
instead of:

When divActive is false:

<div class="makeStyles-div-2"></div>

When divActive is true:

<div class="makeStyles-div-2 makeStyles-div-2__active"></div>

Can you help me figure out what could possibly be missing? I attempted enclosing classes.div in an additional template literal like so:

<div className={`${classes.div}` + divActive ? `${classes.div}__active` : ''}></div>

However, the outcomes remain unchanged. Interestingly, upon removing the initial class (classes.div), the toggle for divActive appears to be functioning correctly by switching between active and inactive states. This leads me to believe that the issue lies within the first variable - but where exactly is the error?

Answer №1

To enhance readability, enclose the ternary condition in parentheses and include extra space when the function divActive evaluates to true

<div className={classes.div + (divActive ? ` ${classes.div}__active` : '')}></div>

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

Encountered an issue while attempting to inflate the class com.google.android.material.bottomnavigation.BottomNavigationView on a L

Encountering an error using com.google.android.material.bottomnavigation.BottomNavigationView in the AndroidX library specifically on devices with Lollipop, yet the same code runs successfully on higher versions of Android. Despite trying various solutions ...

The art of working with callback functions in React.js

I have a function A that triggers when a number is clicked. Function A updates the state of a variable and then calls another function B as a callback. Function B needs to make a redux call to fetch data from an API, which will be used by function C to gen ...

Having trouble converting Buffered Byte Array into Image in Browser

Hello there! I am currently facing an issue while attempting to generate an image from a buffered byte array. The reason behind this is that I need to transfer the image from the server to the client using JSON. Below is the code snippet in question: The ...

Using angularJS and Regular Expressions to eliminate the "+" symbols in an email address

I have been given the task of disallowing email addresses with a "+" sign from registering. As someone new to regex, my understanding is that it is better to focus on defining what constitutes a valid input rather than listing what should be excluded. I re ...

When the browser window is minimized, the top section of the website becomes concealed

For the past few weeks, I have been working on creating a new website. The progress is looking good overall, but there is one issue that I have encountered. When viewing my website in a browser window smaller than the actual webpage size, the top menubar ...

The background color of the body is being utilized by Div in Internet Explorer version 7

Within my HTML document, I have created a container div that holds several nested divs for a slideshow effect. This "container" div is positioned over the background image of the body element. The CSS styling for the body element is as follows: body { ba ...

Tips for saving the ajax response to the browser cache using jquery

I am currently working on storing the ajax response in the browser cache. I have successfully managed to store it for URLs like (example.com), but now I am facing a challenge with storing data for URLs like (example.com?xyz=abc%20efg&mno=hjk). Here is ...

Tips for achieving horizontal alignment of headers

My webpage has two headers positioned as 'CompanyName' on the top left and 'Date' at the top middle but I am struggling to align them horizontally. I attempted to enclose them inside a div element, however, this did not work. Can someon ...

Prevent any future dates from being entered

My Thoughts: <input type="text" class="datepicker" placeholder="DD/MM/YYYY" id="datepicker" ng-model="datepicker" name="datepicker" style="width:100%" tabindex="4" required/>` Controller: `$('#datepicker').datepicker({ format: &a ...

Is there a way to position my input at the bottom of its parent element?

Is there a way to position this input element at the bottom and center of its parent div? ...

I am experiencing an issue where the close button image on tap is not closing on the first tap on

Why does the page close on the second tap instead of the first tap when using an iPhone? The image changes on tap but doesn't close as expected. $("#privacy-close-btn").mouseenter(function() { $("#privacy-close-btn").css("display", "none"); $(" ...

retrieve only the following occurrence throughout the entire file

I am looking to target only the first occurrence of an element in the document after a clicked element. Here is my current approach: $('.class').click(function(){ var x = $(this).nextAll('.anotherclass').first(); //do something ...

Struggling to upload an XLSX file in React using Ant Design?

I am currently in the process of learning React and I have encountered an issue with my upload button while using ant design. The button seems to work fine without the design element. Here is my original working code: export default ({ addItem }) => { ...

What is the process for appending a file extension to a Next.js URL?

For instance, I am attempting to redirect the URL : https://example.com/data/publications to this : https://example.com/data/publications.json I made an attempt using Next.js redirection, but encountered difficulty when trying to add a string that does no ...

Iterating through textboxes and buttons to trigger actions in JavaScript

Having an issue with JavaScript (or jQuery) where I can successfully input text and click a button on a page using the following script: document.getElementsByName('code')[0].value='ads0mx0'; document.getElementsByName('event&a ...

How do I change the alignment of X axis labels in an Asp.net chart to be left-aligned instead of centered?

I've been struggling for hours trying to figure out this simple issue. I am working on creating a histogram using the asp chart control. My main problem is that I can't seem to get the x-axis label to appear on the left side of each column instea ...

Having trouble with CSS margins behaving unexpectedly

Could someone please explain why I am unable to add margin-top/bottom or padding-top/bottom to this "a" tag? I am trying to center "Konoha" in the header box. html{ background-color: white } body{ width: 88.5%; height: 1200px; margin: 0 auto; borde ...

Can you explain the function of this.$nextTick()?

I'm a beginner with Vue.js and I'm trying to wrap my head around the logic of using nextTick in a method. I have two components set up like this: IsTuruIslem.vue <template> ... <t-row> <is-turu-islem-goruntule ref="isTuru ...

Issue with CSS: Dropdown menu is hidden behind carousel while hovering

I'm struggling with adjusting the position of my dropdown list. It keeps hiding behind the carousel (slider). When I set the position of the carousel section to absolute, it causes the navbar to become transparent and the images from the carousel show ...

What is the best way to enable autocomplete in AngularJS?

I am working with an object that contains both a name and an ID. I want to implement autocomplete functionality based on the name property. Below is the code snippet that I have tried: //Js file var app=angular.module("myapp",[]); app.controller("controll ...