onMouseOver event not working in React

Jared Gilpin picture Jared Gilpin · Jul 25, 2018 · Viewed 11k times · Source

The onMouseOver event does not seem to trigger, no matter what I try. I can see that its bound to the component, but nothing happens when I mouse over. onClick works as expected. Where am I going astray?

The code itself is a simple image gallery constructor that calls a 'Gallery' function from react-photo-gallery.

Answer

Anshul Bansal picture Anshul Bansal · Jul 25, 2018

You need to attach hover event on the wrapper div. The external Gallery componet might not be handling the hover event passed as prop.

handleHover = () => {
  console.log("mouse over");
}
<div onMouseOver={this.handleHover}>
          <Gallery
              photos={this.props.pageData.photos}
              onClick={this.openLightbox}
              // onMouseLeave={this.onMouseLeaveHander}
            />
</div>