How can you achieve either a hover event or active event in ReactJS when you do inline styling?
I’ve found that the onMouseEnter, onMouseLeave approach is buggy, so hoping there is another way to do it.
Specifically, if you mouse over a component very quickly, only the onMouseEnter event is registered. The onMouseLeave never fires, and thus can’t update state… leaving the component to appear as if it still is being hovered over. I’ve noticed the same thing if you try and mimic the “:active” css pseudo-class. If you click really fast, only the onMouseDown event will register. The onMouseUp event will be ignored… leaving the component appearing active.
Here is a JSFiddle showing the problem: https://jsfiddle.net/y9swecyu/5/
Video of JSFiddle with problem: https://vid.me/ZJEO
The code:
var Hover = React.createClass({ getInitialState: function() { return { hover: false }; }, onMouseEnterHandler: function() { this.setState({ hover: true }); console.log('enter'); }, onMouseLeaveHandler: function() { this.setState({ hover: false }); console.log('leave'); }, render: function() { var inner = normal; if(this.state.hover) { inner = hover; } return ( <div style={outer}> <div style={inner} onMouseEnter={this.onMouseEnterHandler} onMouseLeave={this.onMouseLeaveHandler} > {this.props.children} </div> </div> ); } });