You have two different div in the DOM that do not overlap; I split render to make it more obvious:
render() { return ( <div> <div className={styles.listTitle} onMouseEnter={this.handleHoverOn} onMouseLeave={this.handleHoverOff}> {this.props.name} </div> <div> {React.Children.map(this.props.children, function(child){ return React.cloneElement(child, {className: 'child'}); })} </div> </div> ); }
div to which onMouseLeave attached onMouseLeave not contain children; therefore, when the mouse moves to hover over a child, it leaves a call to div and this.handleHoverOff .
You can use CSS to hide children if they should not be displayed or conditionally execute them:
render() { return ( <div className={styles.listTitle} onMouseEnter={this.handleHoverOn} onMouseLeave={this.handleHoverOff}> {this.props.name} {this.state.expanded && this.renderChildren()} </div> ); }, renderChildren() { return ( <div> {React.Children.map(this.props.children, function(child){ return React.cloneElement(child, {className: 'child'}); })} </div> ); }
Michelle tilley
source share