onMouseOver event not working in React
Asked Answered
V

2

6

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.

class ShowGallery extends React.Component {
  constructor(props) {
    super(props);
    this.state = { currentImage: 0 };
    this.closeLightbox = this.closeLightbox.bind(this);
    this.openLightbox = this.openLightbox.bind(this);
    this.gotoNext = this.gotoNext.bind(this);
    this.gotoPrevious = this.gotoPrevious.bind(this);
    this.gotoImage = this.gotoImage.bind(this);
    this.onMouseEnterHandler = this.onMouseEnterHandler.bind(this, 'value');
    this.onMouseLeaveHandler = this.onMouseLeaveHandler.bind(this, 'value');
  }
  openLightbox(event, obj) {
    this.setState({
      currentImage: obj.index,
      lightboxIsOpen: true
    });
  }
  closeLightbox() {
    this.setState({
      currentImage: 0,
      lightboxIsOpen: false
    });
  }
  gotoPrevious() {
    this.setState({
      currentImage: this.state.currentImage - 1
    });
  }
  gotoNext() {
    this.setState({
      currentImage: this.state.currentImage + 1
    });
  }
  gotoImage(event, obj) {
    this.setState({
      currentImage: obj.index
    });
  }
  onMouseEnterHandler (event, obj) {
    console.log("mouse entered")
  }
  onMouseLeaveHandler (event, obj) {
    console.log("mouse left area")
  }
  render() {
    return (
      <div>
        <Container>
          {this.props.pageData.sectionTitle === "hello" ?(<SectionHeader
            title={this.props.pageData.sectionTitle}
            paragraph={this.props.pageData.blurb}
          />) : null}

          <Col>
          </Col>
        </Container>
        <Row>
          <Col>
            <Gallery
              photos={this.props.pageData.photos}
              onClick={this.openLightbox}
              onMouseOver={() => console.log("mouse over")}
              // onMouseLeave={this.onMouseLeaveHander}
            />
            <Lightbox
              images={this.props.pageData.photos}
              onClose={this.closeLightbox}
              onClickPrev={this.gotoPrevious}
              onClickNext={this.gotoNext}
              currentImage={this.state.currentImage}
              isOpen={this.state.lightboxIsOpen}
              backdropClosesModal={true}
            />
          </Col>
        </Row>
      </div>
    );
  }
}

export default ShowGallery;

 
 
Venola answered 25/7, 2018 at 19:44 Comment(3)
Is Gallery a component from an external library, or is it your own component?Polynesia
Its an external library that creates a series of img tags contained in a div.Venola
Try wrapping the <Gallery /> in a <div> and put the mouseOver/mouseEnter on the div instead.Deduct
A
7

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>
Aqueous answered 25/7, 2018 at 19:55 Comment(3)
Out of curiosity, why would an onClick work as a prop, but a onMouseOver would not?Venola
It depends on the implementation of the third party module you are using(Gallery) in your project. The module supports click handler in your case. You will have a better clue about what all props are supported if you read through the 3rd party module's documentation.Aqueous
Thanks. I looked at the code and I can see a click handler added in. Makes sense now.Venola
B
1

It doesn't appear as though react-photo-gallery implements anything other than an onClick handler.

React components don't handle the same events that native elements handle by default; they have to provide some kind of handler to those events themselves.

The Gallery component allows you to specify a handler for click events. When the elements rendered by the Gallery component receive click events, the handler that you provided will be called.

Bojorquez answered 25/7, 2018 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.