I'm trying to simulate a hover event in React.
So I am using onMouseEnter
and onMouseLeave
that triggers the statechange.
Unfortunately when I first enter the Element with the mouse both methods are triggered...
When I leave the Element only onMouseLeaveis triggered.
Does anyone knows why the method
onMouseLeave` is triggered on entering the element.
Here is my Code:
handleOver(event){
event.preventDefault();
console.log('enter');
if(this.state.open === false){
this.setState({
open: true,
anchorEl: event.currentTarget,
});
}
}
handleLeave(event){
event.preventDefault();
console.log('leave');
if(this.state.open === true){
this.setState({open: false})
}
}
<img
onMouseEnter={this.handleOver}
onMouseLeave={this.handleLeave}
src={InfoIcon}
/>
<Popover
open={this.state.open}
anchorEl={this.state.anchorEl}
anchorOrigin={{horizontal: 'right', vertical: 'top'}}
targetOrigin={{horizontal: 'left', vertical: 'top'}}
onRequestClose={this.handleLeave}
muiTheme={darkBaseTheme}
/>
Technically, this is not a bug, since the onMouseEnter
event is triggered correctly, which causes the Popover
component to render with a backdrop over the page, which triggers the onMouseLeave
event.
This "issue" is discussed at [Popover] event handlers issue (mouseEnter and mouseLeave) #7680.
Credit goes to konradwww, for listing the following explanation, that leads to the behavior any sane person would expect:
<Popover
style={{ pointerEvents: 'none'}}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With