Having difficulty toggling a class in my ReactJS project, it seems to be ineffective

I'm having trouble toggling a class to change the display value to none in my React.js project. Can anyone help troubleshoot this issue?

function sleep(milliseconds:any) {
          return new Promise((resolve) => {
            setTimeout(resolve, milliseconds);
          });
     }

     async function setHeight(el:any) {
          const size = el.current!.scrollHeight === 288;
          el.current!.style.setProperty("width", (size) ? "38rem" : "18rem")
          el.current!.style.setProperty("height", (size) ? "38rem" : "18rem")
          await sleep(500);
          circle.current!.classList.toggle("hidden")
     }

CSS:

.hidden{
     display: none !important;
}

Answer №1

To ensure proper updates of the component, it is recommended to implement a life cycle method or hook such as useState and useEffect in order for React to trigger re-rendering.

Answer №2

Encountered another issue personally, and it dawned on me that I should have used styles.hidden instead of simply using "hidden". Grateful for the helpful responses provided.

circle.current!.classList.toggle(styles.hidden)

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

Understanding the NavigationContainer reference in Typescript and react-navigation

In my current project with react-navigation, I've come across a scenario where I need to navigate from outside of a component (specifically after receiving a push notification). The challenge is that when I use the navigation.navigate method from wit ...

Is it possible to spread an empty array in JavaScript?

Whenever I run the code below, I encounter the error message Uncaught SyntaxError: expected expression, got '...': [1,2,3, (true ? 4 : ...[])] I'm wondering if spreading an empty array in that manner is allowed? ...

Challenges with utilizing Ionic Native in a cross-platform application

I am currently developing an application using Ionic 2 that can function both as a website in a browser and as a mobile app on iOS and Android. One key aspect of the app is its use of the SQLite plugin when accessed on mobile devices. However, I have encou ...

What is the best way to eliminate the alert message "autoprefixer: Greetings, time traveler. We are now in the era of CSS without prefixes" in Angular 11?

I am currently working with Angular version 11 and I have encountered a warning message that states: Module Warning (from ./node_modules/postcss-loader/dist/cjs.js): Warning "autoprefixer: Greetings, time traveler. We are now in the era of prefix-le ...

Spacing between hidden checkboxes and their labels

I've customized some checkboxes to resemble a select dropdown by hiding the input and using only the label as the visible element. However, I'm struggling to add margin between the elements, which seems to work only when the checkbox is visible. ...

Instructions for creating a fading effect on W3schools Modal example

After utilizing the CSS class from https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_modal_fade to achieve a fade in effect, I am now looking for a way to fade out when the modal closes. How can this be accomplished? .w3-animate-opacity{animatio ...

Display 3 images with the carousel feature on the middle image

Currently, I am utilizing the jquery.jcarousellite plugin and attempting to make the second image active. Below is the code snippet I have tried: <div id="jcl-demo"> <div class="custom-container auto moreItems "> <a href="#" c ...

The parameter type '(value: any) => any[]' does not match the expected parameter type 'string[]'

I'm encountering an issue where I can't push a value to a state array due to a TS error. Let me highlight the relevant components where the error occurs. The error specifically lies in the child component, within the handleValue(value => [...v ...

Is there a quicker alternative to the 500ms delay caused by utilizing Display:none?

My div is packed with content, including a chart from amCharts and multiple sliders from noUiSlider. It also features various AngularJS functionalities. To hide the page quickly, I use $('#container').addClass('hidden'), where the rule ...

Checking if a start date comes after an end date using Yup - how is it done?

How can I use Yup to validate if the start date overlaps with the end date in a form that creates an event using Formik library? I have two date pickers for choosing the dates and times. I want to display an error message if they overlap. Thanks for the ...

What about nested elements with percentages, set positions, and fixed placements?

Picture: https://i.sstatic.net/KFNR1.png I am working on a design with a yellow container div that I want to keep at 50% width of the window. Inside this container is a purple image div that stretches to 100% of the parent container's width, and ther ...

Will my JavaScript function be triggered when the page is resized while printing?

I have a simple HTML layout where I am using the TextFill jQuery library to automatically adjust the text size based on available space. Everything looks good on screen, but when printed, it seems like the page is resized and the JavaScript needs to be re ...

Steps to insert a new row for a grid item using material-ui

When it comes to breaking a line of grid item like this, the image shown below illustrates how the rest space of the grid should be empty. https://i.stack.imgur.com/pvSwo.png <Grid container spacing={3} <Grid item xs={12}> "Grid Item xs ...

Ways to switch text on and off smoothly using transitions

I have created a webpage where text starts off hidden but has a visible heading. When you click on the heading, the text is revealed below. I am putting the final touches and aiming to make the text slide in smoothly. I am using Javascript for toggling ins ...

Proper approach for mapping JSON data to a table using the Fetch API in a React.js application

Struggling to map some elements of JSON to a Table in Customers.jsx, but can't seem to figure out the correct way. How do I properly insert my Fetch Method into Customers.jsx? Specifically managing the renderBody part and the bodyData={/The JsonData/} ...

Tips on modifying the background and font colors of the Dropdown within the TimePicker component in MUI React

I've been attempting to modify the background color and font color of the TimePicker using the css className properties, but I haven't had any luck. Is there anyone who knows how to achieve this and where can I find documentation on these propert ...

File type cannot be determined on certain devices

I am currently utilizing the antd Upload component in React to handle file input. My specific scenario only allows certain types of files to be uploaded. To enforce this restriction, I am employing the beforeUpload prop of the Upload component to verify th ...

What is the best way to target the final n children using CSS?

Is there a way in CSS to target and style the last N elements of a parent container? The number of elements we want to match is not fixed, but could vary. How can this be achieved using CSS? ...

How to specify the return type of a promise from an observer in Angular 6

Typically, I prefer using observables. However, in order to avoid 'callback hell' in this particular scenario, I decided to use toPromise(). Unfortunately, I encountered a lint error message when trying to define the return type: The 'Obj ...

What is the best method for incorporating an image upload field into Redux Form?

How can I effectively create a submit form using Redux-Form that includes an image upload field among other text fields? I have encountered a problem with the current approach where the form gets re-rendered when attempting to upload an image. What is th ...