Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent onmouseout from firing on child elements

Tags:

reactjs

Given the following:

render() {

  const onMouseOver = (event) => {
    this.setState({ isHovering: true });
  };

  const onMouseOut = (event) => {
    this.setState({ isHovering: false });
  };

  const open = this.state.isHovering ? true : false;

  return (
    <Nav className={styles.TopNav} bsStyle="pills" activeKey={1}>
      <NavDropdown
        className={dropDownClasses}
        eventKey={1}
        open={open}
        title="Cards"
        id="nav-dropdown"
        onMouseEnter={onMouseOver}
        onMouseOut={onMouseOut}>
        <MenuItem eventKey="1.1">Action</MenuItem>
        <MenuItem eventKey="1.2">Another action</MenuItem>
      </NavDropdown>
      <NavItem className={styles.navLink} eventKey={2}>Blog</NavItem>
    </Nav>
  );

How do I prevent onMouseOut firing when the mouse is over a child of NavDropdown.

Can I detect if I am over a child in onMouseOut?

like image 565
SystemicPlural Avatar asked Dec 03 '15 16:12

SystemicPlural


People also ask

What is the difference between Onmouseleave and Onmouseout?

The onmouseleave event is only supported by Internet Explorer, for a cross-browser solution, use the onmouseout event. The only difference between the onmouseleave and onmouseout events is that the onmouseout event propagates up the document hierarchy, while the onmouseleave does not.

What is the difference between mouseover and Mouseenter?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

What does Onmouseout mean?

Definition and Usage The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children. Tip: This event is often used together with the onmouseover event, which occurs when the pointer is moved onto an element, or onto one of its children.

What is Onmouseover and Onmouseout?

Definition and Usage The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.


1 Answers

This is not React specific. mouseover and mouseout events bubble, so the handler also triggers for children of the element. mouseenter and mouseleave don't bubble.

So you should listen to mouseleave instead.

like image 121
Felix Kling Avatar answered Sep 20 '22 15:09

Felix Kling