How do you Hover in ReactJS? - onMouseLeave not registered during fast hover over

前端 未结 13 2288
旧时难觅i
旧时难觅i 2020-11-28 04:23

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

13条回答
  •  一个人的身影
    2020-11-28 04:47

    I've just bumped into this same problem when listening for onMouseLeave events on a disabled button. I worked around it by listening for the native mouseleave event on an element that wraps the disabled button.

    componentDidMount() {
        this.watchForNativeMouseLeave();
    },
    componentDidUpdate() {
        this.watchForNativeMouseLeave();
    },
    // onMouseLeave doesn't work well on disabled elements
    // https://github.com/facebook/react/issues/4251
    watchForNativeMouseLeave() {
        this.refs.hoverElement.addEventListener('mouseleave', () => {
            if (this.props.disabled) {
                this.handleMouseOut();
            }
        });
    },
    render() {
        return (
            
                
            
        );
    }
    

    Here's a fiddle https://jsfiddle.net/qfLzkz5x/8/

提交回复
热议问题