I have this code in react, and I hope that when I hover over a section, the function onMouseOver
that I pass by props to the component is executed. This function just prints a console.log of the e.target
. The thing is that if I hover over the section, I correctly see <section id="amenities"... />
, but if I hover over any inner element of this section, now the inner element is consoled. Is there a way to ALWAYS get as e.target
the section
instead of its inner elements?
<section
className="resort-amenities"
id="amenities"
onMouseEnter={onMouseOver}
>
<h2 className="title">Amenities</h2>
<div className="amenities-wrapper">
{renderAmenities}
</div>
</section>
ISSUE
The problem is that you are using
e.target
:This way, hovering over any element within your
<section>
will delegate the event from the parent to the children.SOLUTION
The solution is to use
e.currentTarget
that:That is, even if the event of hovering over your element
<section>
occurs on any child of it,currentTarget
it will always be the parent, that is, your element<section>
.I hope this helps you solve the problem.