Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onMouseOver event not working in React

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;

 
 
like image 488
Jared Gilpin Avatar asked Jul 25 '18 19:07

Jared Gilpin


1 Answers

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>
like image 86
Anshul Bansal Avatar answered Sep 23 '22 03:09

Anshul Bansal