If you want to handle a click event on the parent element, keep in mind that this will not accurately determine if the click occurred specifically on the "x" button.
Pseudo elements should primarily be used for decorative purposes. Because they are styled using CSS and do not require HTML markup, they are not designed for functional actions such as button clicks.
Instead of relying on pseudo elements for interactive elements like an "X" button, consider using a proper button element. Pseudo elements lack accessibility features like keyboard focus, making them less suitable for functional components.
class PseudoExample extends React.Component {
render () {
return (
<div>
<button onClick={this.close} type="button" class="close"/>
</div>
)
}
close = () => {
console.log('clicked')
}
}
ReactDOM.render(<PseudoExample/>, document.querySelector('main'))
.close {
border: none;
}
.close:after {
content: "X";
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<main/>
I understand the frustration with "you're doing it wrong" responses. However, in this scenario, making a minor adjustment (hopefully not too extensive) may prove to be a more effective solution.