Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

css - How to change style of an element in React without using querySelector?

I am new to the React. Here, I'm trying to override 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)`);

Here, I am managing the state of this variable and I am calling toggleFilter on the button click

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

But I don't want to use document.querySelector(".main-table-body") to fetch the class, Is there any other way of doing this. I am using Material-UI too. If that helps

question from:https://stackoverflow.com/questions/65516915/how-to-change-style-of-an-element-in-react-without-using-queryselector

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the render of the main-table-body, check the state to determine what CSS property it should have, eg:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...