Im trying to implement hover event but onMouseLeave is not always triggering when leaving element especially when moving cursor over elements fast. I tried i Chrome, Firefox and Internet Explorer but in every browser same problem appeared.
My code:
import React from 'react';
import Autolinker from 'autolinker';
import DateTime from './DateTime.jsx'
class Comment extends React.Component{
constructor(props){
super(props);
this.handleOnMouseOver = this.handleOnMouseOver.bind(this);
this.handleOnMouseOut = this.handleOnMouseOut.bind(this);
this.state = {
hovering: false
};
}
render(){
return <li className="media comment" onMouseEnter={this.handleOnMouseOver} onMouseLeave={this.handleOnMouseOut}>
<div className="image">
<img src={this.props.activity.user.avatar.small_url} width="42" height="42" />
</div>
<div className="body">
{this.state.hovering ? null : <time className="pull-right"><DateTime timeInMiliseconds={this.props.activity.published_at} byDay={true}/></time>}
<p>
<strong>
<span>{this.props.activity.user.full_name}</span>
{this.state.hovering ? <span className="edit-comment">Edit</span> : null}
</strong>
</p>
</div>
</li>;
}
handleOnMouseOver(event){
event.preventDefault();
this.setState({hovering:true});
}
handleOnMouseOut(event){
event.preventDefault();
this.setState({hovering:false});
}
newlines(text) {
if (text)
return text.replace(/\n/g, '<br />');
}
}
export default Comment;
seems to be an issue caused by event delegation when the event listener is on the parent element and child elements are being conditionally added/removed from the DOM. putting a "hover target" component that sits on top of everything should make this work properly, but could cause other issues if you need to click elements inside.
<Container isOpen={this.state.isOpen}>
<HoverTarget
onMouseEnter={e => this.mouseOver(e)}
onMouseLeave={e => this.mouseOut(e)}
/>
<Content/>
</Container>
mouseOver(e) {
if (!this.state.isOpen) {
this.setState({ isOpen: true });
}
}