Using ReactJS and SCSS to adjust the height: set the height to be twice the size of the element's

Is there a way to increase the height of a div each time it is clicked?

I had an idea to add an extra CSS class to a div whenever a button is clicked. Here's how the concept might look:

#divToBeDoubled {
    height: 100px;

    &.doubleHeight{
        height: selfHeight*2
    }
}

Can this be accomplished using Reactjs and SCSS?

Answer №1

If you are looking to update a CSS value based on the component state, consider moving the desired property within your component or utilizing JSS...

One way to achieve this is by relocating the property within the render method:

class MyComponent extends React.Component{

  state = {
    divHeight : 100
  }

  doubleSize = ()=>{
    this.setState({ divHeight : this.state.divHeight * 2 })
  }

  render() {

    const style = {
      height: this.state.divHeight
    };

    return (
      <div style={style} onClick={this.doubleSize}>Content</div>
    );
  }

}

Answer №2

When incorporating sass into your project, it is possible to utilize the following code snippet that allows you to toggle classes in JSX by clicking on an event.

$dynamicHeight: 100px;
.regular {
    height: $dynamicHeight;
}
.doubled {
    height: $dynamicHeight * 2;
}

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

Bootstrap - Border padding excessively wide

For a project exercise using bootstrap, I'm working on recreating a page that can be found here: https://codepen.io/freeCodeCamp/full/NNvBQW I've hit a roadblock in trying to put a border around the image. Here's the code I have so far: htt ...

Displaying the content of a modal window

I am working on a modal that needs to print only the content within it, nothing else. Here is the setup for the button inside the modal: This should not be printed... <button id="btnPrint">Print (this btn should not be printed!)</button> ...

Eliminate the empty gap preceding the slideshow

I have a slideshow and aside content in HTML. My goal is to eliminate the space below the slideshow so that the aside content can be positioned right next to the end of the slideshow. Unfortunately, I am unsure how to remove this space without disrupting ...

When the screen size decreases, HTML images do not adjust accordingly

Hey there, I'm just diving into the HTML and CSS world with FreeCodeCamp's curriculum. The second project involves creating a survey/form, which I've managed to put together quite nicely--except for one small issue with the images. While the ...

Place two divs side by side with the second div filling up the remaining space on the line

Hello there, can anyone lend a hand with this problem? This is the code I have been working with: <html> <body> <div style="width=100%"> <div style="float:left; background-color:Red; height:100px">Red</div> <div st ...

Utilizing the useState hook in Recharts' Brush component's onChange function impedes the Brush from updating its value

My goal is to capture the startIndex and endIndex values from the Brush component and store them in the state. However, using the "useState" hook seems to lock the default values in place, preventing the user from interacting with the Brush. Even though t ...

What is causing some Font Awesome icons to not function properly with boostrapCDN?

I thought all Font Awesome icons would work by linking this bootstrap stylesheet, but I've only managed to get one of them (`.fa fa-deskto`) to display. The rest, like the second one for example (link below), simply don't show up: Both icons are ...

Optimizing the integration of Antd and SCSS in a Nextjs project to seamlessly resolve any style loading complications

In my Next.js project, I am utilizing Antd for UI components and SCSS for styling. However, the styles are not loading properly despite trying various solutions like modifying the next.config.js file. Despite setting cssModules to false in the next.config ...

Handling events in React using TypeScript

Currently diving into the world of React with Typescript and encountered a challenge involving event handling using the onClick property. I have a react component displaying a list of items from an array, and I aim to log the clicked item in the console. I ...

TypeScript function object argument must be typed appropriately

In the code, there is a function that I am working with: setTouched({ ...touched, [name]: true }); . This function accepts an object as a parameter. The 'name' property within this object is dynamic and can be anything like 'email', &ap ...

Trouble with Alignment in Bootstrap 3 Form

I'm currently facing an issue with aligning a form group in Bootstrap 3. My goal is to have them aligned vertically, but I am struggling to achieve this. Any help would be greatly appreciated! Please refer to the screenshot below for reference: Her ...

Listening for an event or using a CSS pseudo-class to detect when the scrollbar becomes

Are there any JavaScript event listeners or CSS pseudo classes that can detect when a scrollbar appears and disappears? For example, on Mac OS and Windows Internet Explorer 10 or newer, the scrollbars are hidden by default but appear when scrolling begins. ...

Enhancing a character's appearance with custom CSS styling

In my code, I want to highlight a single character throughout the page. I want the lines between each word to be a different color from the rest of the text using CSS, without having to add span tags to each one individually. Anti-virus End Point | Dis ...

The React Router onEnter Function Error: "Maximum call stack size exceeded"

I'm currently checking if a specific configuration value is set, and if it is, I want to redirect to a child route. If not, the page should display as usual. However, when I implement this code, the URL updates as expected but then seems to get stuck ...

Flexibility on smaller screens using Flexbox

On larger screens, I have arranged 4 icons in a row, but on smaller screens (less than 768px), I need to display only 2 icons in a row. This is my current code : .welcome { display: flex; justify-content: space-around; } @media (max-width: 768px) ...

Tips on removing an object array in Node.js

I am working on a basic to-do list application using React.js and Node.js without involving a database. I am able to delete elements one by one from the front end, but I need help with deleting them from the Node backend. Can anyone assist me with this? t ...

Could it be that this.setState is not doing anything to update my state?

I am currently in the process of developing a React application for a shopping list. My goal is to manage the list items as state. After submitting the input to add a new item, a function is triggered which passes the new list to the App and attempts to re ...

What is the most efficient method for saving Redux state into local state using React JS?

In my application, I have a reducer called tables which includes a state named mainTables. I am utilizing the react-dnd library to manage the positioning of these tables. const initialState = { channel: null, errors: null, mainTables: [], takeaways: [ ...

A table featuring an HTML select element that allows users to choose from preset options or manually enter

My goal is to incorporate a select box where users can either choose from a set list of options or input their own custom value. The select element is nested within a table. Unfortunately, using datalist is not a viable solution for me in this case. I have ...

Increasing height for individual Bootstrap cards, without affecting all cards within the same row

I've encountered an issue where I need to increase the height of a single bootstrap card in a tile-module layout without affecting the height of any other cards in the same row. The code snippet below shows the HTML structure I currently have: <div ...