I'm facing a situation where clicking on a parent element results in it flipping to display a child element with different colors. However, the issue arises when the user tries to click on one of the colors in the child element, as it also triggers the 'click' event on the parent element.
Is there a way to prevent the event from being triggered on the parent when the child is clicked?
Here are some possible solutions that I'm considering:
Using CSS?
Adding a classpointer-events : none
to the parent when the child is clicked. However, this would require removing thepointer-events
class from the parent later on.Utilizing Refs?
Storing theref
of the parentReact
element and comparing theevent.target
with the ref when the child is clicked. I have some reservations about this approach due to the global nature of refs.
I would greatly appreciate any thoughts and suggestions for a better solution to the following question: How can I prevent the event from being triggered on the parent when the child is clicked?