Is there a way to alter the styling of a React element without resorting to querySelector?

Just diving into React and I'm faced with a challenge - how to update the CSS of a class based on a condition.

let customizeColumn = document.querySelector(".main-table-body");
!expandFilter ? customizeColumn.setAttribute("style", `height:calc(100vh - 345px)`) : customizeColumn.setAttribute("style", `height:calc(100vh - 302px)`);

Currently, I am keeping track of the state of 'expandFilter' and toggling it with a button click.

const [expandFilter, toggleFilter] = useState(false);

However, I'd prefer not to rely on

document.querySelector(".main-table-body")
for accessing the class. Is there an alternative method that doesn't involve directly grabbing the element? Also, I am utilizing Material-UI, if that offers any solutions.

Answer №1

When rendering the main-table-body, it is important to check the state in order to determine the appropriate CSS property for it, such as:

return (
  <table 
    class="main-table-body"
    style={{ height: `calc(100vh - ${showFilters ? 302 : 345}px)` }}
  >
    // content of the table
  </table>
);

Answer №2

To add style, simply use the style attribute.

Here's an example of how you can do this in the render method:

const pixelsToReduce= expandFilter ? 302 : 345
const height=`calc(100vh - pixelsToReduce)`
return (
  <table
     class="main-table-body"
     style={{height:height}}
  >
  </table>
)

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

Validate prop must consist of one of two functional components

I am looking to ensure that a prop can only be one of two different components. Here is what I currently have: MyComponent.propTypes { propA: PropTypes.oneOfType([ PropTypes.instanceOf(ClassComponentA) PropTypes.instanceOf(ClassCompon ...

When inserting a page break after a section, it seamlessly flows to the next page during printing

When should the CSS properties page-break-after: always or before be used? I have tried various methods, such as creating different div elements for page-break, adjusting float and clear:both, but have not had any success. There are currently four section ...

Tips for aligning two divs vertically and horizontally next to each other

I'm struggling to center two divs both horizontally and vertically at the top of my page. I've tried various methods like "vertically align" & "margin: auto 0", but nothing seems to work. Can anyone help me figure out what I'm doing wron ...

The text inside the Mapbox GL popup cannot be highlighted or copied

I'm encountering an issue where the text in my popups is unselectable. Even though I can close the popups through various methods, such as clicking on them, the pointer remains as a hand icon when hovering over the text and doesn't change to the ...

Is there a way to activate an Electron API using a remote server?

Hello all, I am relatively new to Electron development and have an interesting idea that I want to implement: My goal is to send a request to an express server from a website outside of my electron app along with some data. I then want the express route t ...

Troubleshooting: Next JS tab indicator does not work when using tabs with links

I have been experimenting with Next js and material UI, and here is what I've come up with so far <Tabs value={value} onChange={handleChange}> <Link href="/" passHref> <Tab label="Home"/> </Link> ... </Ta ...

What sets React.FC<T> apart from Function() in TypeScript?

Do arrow functions and function notations differ from each other? It seems like these two methods function in the same way. One is written as React.FC<T> while the other as Function(). Is this simply a matter of notation, or are there deeper reaso ...

Tips for arranging divs in CSS grid with grid-template-areas technique

I am attempting to organize similar groups of elements in a grid: #grid { position: absolute; left: 135px; top: 158px; width: 1080px; height: 1035px; display: grid; grid-template-rows: 99px 1fr 1fr; grid-template-column ...

Is there a way for me to keep an image stationary on the page while allowing overlaying text to move?

Currently, I am exploring the process of coding a website that mimics the style of this particular webpage: . I am particularly interested in learning how to create a scrolling effect for text within a fixed area, while keeping the accompanying images st ...

Add value to input only if there is data available using a React functional component

Featuring an input field as part of a questionnaire with next and back buttons: <input id="answer"/> Capturing the user's input value when they click on the next or back buttons: const [quizIndex, setIndex] = useState(0) const [answe ...

What is the process for obtaining a push key in Firebase?

When a user submits data to the Firebase database, I need to handle the Key generated by that action in my own function. The challenge arises when a user fills out a form and sends data to our real-time DB. This data may include optional images, and I wan ...

Using React.js - What is the process for submitting a form automatically when the page loads?

Upon loading the page, I have a form that needs to be displayed. Here is the code snippet: <form name="myform" method="POST" className={classes.root} target="mydiv" action="https://example.com/submit" onLoad= ...

Is there a way to resolve the issue of my localStorage getting overridden on page refresh?

I am currently working on a simple React app and encountering an issue with the localStorage. Whenever I refresh the page, the todo list stored in the localStorage disappears. How can I ensure that the todos remain visible even after a refresh? Any sugges ...

Position a div element on top of Google Maps to create a floating

Currently working on a website and attempting to add shadows over Google Maps by using a <div> element for a shadow effect: Unfortunately, the desired effect is not being achieved. This is the current CSS code in use: html { height: 100% } body ...

Error: The variable "details.date.getTime" is not defined and cannot be accessed

Currently, I am utilizing https://github.com/zo0r/react-native-push-notification to display notifications. Specifically, I am using scheduled notifications with datetimepicker. Previously, I have successfully used this in another project without any errors ...

I was having trouble getting my npm to run properly during initialization. I'm hoping this is not the main issue. Could someone please review my work? It doesn't seem to be rendering as expected

From the start of my certification, I've been struggling with npm and json not working. Two peer reviews of my app had conflicting feedback - one said all features were there while another claimed it was completely unusable. I'm trying to iron ou ...

How can I ensure that Chakra UI MenuList items are always visible on the screen?

Currently, I am utilizing Chakra UI to design a menu and here is what I have so far: <Menu> <MenuButton>hover over this</MenuButton> <MenuList> <Flex>To show/hide this</Flex> </MenuList> </ ...

I need help figuring out how to mention an id using a concatenated variable in the jquery appendTo() method

Using jQuery, I am adding HTML code to a div. One part of this code involves referencing a div's ID by concatenating a variable from a loop. $(... + '<div class="recommendations filter" id="recCards-'+ i +'">' + &apo ...

How to ensure that a React Material-UI Fab Button remains fixed in a specific position on the screen

My project involves creating a CRUD application using React and Material UI. One of the requirements is to have a main screen that displays records in a table format, with a floating Fab button positioned above the table. The challenge is to ensure that th ...

Using multiple header tags in HTML

I have been tasked with developing a webpage featuring a prominent header at the top of the page. This header will include the navigation bar, logo, and website title. As the user begins to scroll, I want the header to transform into a compact version that ...