I have a task that is simple (one line) in plain HTML/CSS/JS, but I can’t find a simple and working method in React.
I have a parent div, and it has two children. The second child is the meaningful content. The first child is a "hide" button, that, when clicked, hides the parent div (including, of course, both child divs).
function SecondPage({ thisData }) {
function hidePage(thisID) {
let thisElement = document.getElementById(thisID)
thisElement.style.display='none';
}
return ( <>
<div className="page" id="InfoPage1">
<div className="hidePageButton" onClick={hidePage("InfoPage1")}>
✖
</div>
<div className="pageContent">
Some text and stuff
</div>
</div>
<div className="page" id="InfoPage2">
<div className="hidePageButton" onClick={hidePage("InfoPage2")}>
✖
</div>
<div className="pageContent">
Some text and stuff
</div>
</div>
</> );
}
export default SecondPage;
In fact, there is an even easier/simpler way using onClick={hidePage(this)}
and function hidePage(el) {el.parentNode.style.display='none'
}
But these methods don’t work in React. I’ve seen some proposed solutions for React that supposedly do what I want, but they are complicated. They propose defining classes with embedded functions and all kinds of seemingly extra baggage to do this simple task.
I’ve also read hints that being able to get elements by IDs and getting parents is not in line with React’s philosophy. But I don’t care about that. I don’t need the best way according to React gospel, or the most modular, or the safest, or anything like that.
What is the simplest possible way to get a div button to hide its parent div (or make other style changes) on click in React (jsx).
I’ve edited the question after receiving a few great answers because they couldn’t obviously handle something closer to my real case: multiple instances per HTML block. One of the points of passing the elementID to the hide function is to tell the hide function which div element to hide (in my case, its immediate parent). I don’t see how to support multiple instances per page with the current proposed solutions.
3
Answers
ref
of parent divonClick
handler to Set parent divdisplay
tonone
Working sample: https://stackblitz.com/edit/vitejs-vite-amkkzf?file=src%2FApp.jsx
More simple way IMO without using ref, just defining parent style
Example
For styling; Use a state that will apply a CSS class. Then add any styling (eg:
display: none
) to the class you’re adding.getElementById
should be avoided in React when possibleref
is overcomplicating thingsMinimal working example:
If you wish to prevent the content from rendering, you could wrap the
div
in a conditional render: