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

前端 未结 13 2252
旧时难觅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:29

    The previous answers are pretty confusing. You don't need a react-state to solve this, nor any special external lib. It can be achieved with pure css/sass:

    The style:

    .hover {
      position: relative;
    
      &:hover &__no-hover {
        opacity: 0;
      }
    
      &:hover &__hover {
        opacity: 1;
      }
    
      &__hover {
        position: absolute;
        top: 0;
        opacity: 0;
      }
    
      &__no-hover {
        opacity: 1;
      }
    }
    

    The React-Component

    A simple Hover Pure-Rendering-Function:

    const Hover = ({ onHover, children }) => (
        <div className="hover">
            <div className="hover__no-hover">{children}</div>
            <div className="hover__hover">{onHover}</div>
        </div>
    )
    

    Usage

    Then use it like this:

        <Hover onHover={<div> Show this on hover </div>}>
            <div> Show on no hover </div>
        </Hover>
    
    0 讨论(0)
  • 2020-11-28 04:31

    Note: This answer was for a previous version of this question where the question asker was trying to use JavaScript to apply css styles… which can simply be done with CSS.

    A simple css-only solution.

    For applying basic styles, CSS is simpler and more performant that JS solutions 99% of the time. (Though more modern CSS-in-JS solutions — eg. React Components, etc — are arguably more maintainable.)

    Run this code snippet to see it in action…

    .hover-button .hover-button--on,
    .hover-button:hover .hover-button--off {
      display: none;
    }
    
    .hover-button:hover .hover-button--on {
      display: inline;
    }
    <button class='hover-button'>
      <span class='hover-button--off'>Default</span>
      <span class='hover-button--on'>Hover!</span>
    </button>

    0 讨论(0)
  • 2020-11-28 04:32

    A package called styled-components can solve this problem in an ELEGANT way.

    Reference

    1. Glen Maddern - Styling React Apps with Styled Components

    Example

    const styled = styled.default
    const Square = styled.div`
      height: 120px;
      width: 200px;
      margin: 100px;
      background-color: green;
      cursor: pointer;
      position: relative;
      &:hover {
        background-color: red;
      };
    `
    class Application extends React.Component {
      render() {
        return (
          <Square>
          </Square>
        )
      }
    }
    
    /*
     * Render the above component into the div#app
     */
    ReactDOM.render(<Application />, document.getElementById('app'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script>
    <script src="https://unpkg.com/styled-components/dist/styled-components.min.js"></script>
    <div id='app'></div>

    0 讨论(0)
  • 2020-11-28 04:35

    I'd use onMouseOver & onMouseOut. Cause in React

    The onMouseEnter and onMouseLeave events propagate from the element being left to the one being entered instead of ordinary bubbling and do not have a capture phase.

    Here it is in the React documentation for mouse events.

    0 讨论(0)
  • 2020-11-28 04:35

    I know It's been a while since this question was asked but I just run into the same issue of inconsistency with onMouseLeave() What I did is to use onMouseOut() for the drop-list and on mouse leave for the whole menu, it is reliable and works every time I've tested it. I saw the events here in the docs: https://facebook.github.io/react/docs/events.html#mouse-events here is an example using https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp:

    handleHoverOff(event){
      //do what ever, for example I use it to collapse the dropdown
      let collapsing = true;
      this.setState({dropDownCollapsed : collapsing });
    }
    
    render{
      return(
        <div class="dropdown" onMouseLeave={this.handleHoverOff.bind(this)}>
          <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
          <span class="caret"></span></button>
          <ul class="dropdown-menu" onMouseOut={this.handleHoverOff.bind(this)}>
            <li><a href="#">bla bla 1</a></li>
            <li><a href="#">bla bla 2</a></li>
            <li><a href="#">bla bla 3</a></li>
          </ul>
        </div>
      )
    }
    
    0 讨论(0)
  • 2020-11-28 04:43

    I personally use Style It for inline-style in React or keep my style separately in a CSS or SASS file...

    But if you are really interested doing it inline, look at the library, I share some of the usages below:

    In the component:

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        return (
          <Style>
            {`
              .intro {
                font-size: 40px;
              }
            `}
    
            <p className="intro">CSS-in-JS made simple -- just Style It.</p>
          </Style>
        );
      }
    }
    
    export default Intro;
    

    Output:

        <p class="intro _scoped-1">
          <style type="text/css">
            ._scoped-1.intro {
              font-size: 40px;
            }
          </style>
    
          CSS-in-JS made simple -- just Style It.
        </p>
    


    Also you can use JavaScript variables with hover in your CSS as below :

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        const fontSize = 13;
    
        return Style.it(`
          .intro {
            font-size: ${ fontSize }px;  // ES2015 & ES6 Template Literal string interpolation
          }
          .package {
            color: blue;
          }
          .package:hover {
            color: aqua;
          }
        `,
          <p className="intro">CSS-in-JS made simple -- just Style It.</p>
        );
      }
    }
    
    export default Intro;
    

    And the result as below:

    <p class="intro _scoped-1">
      <style type="text/css">
        ._scoped-1.intro {
          font-size: 13px;
        }
        ._scoped-1 .package {
          color: blue;
        }
        ._scoped-1 .package:hover {
          color: aqua;
        }
      </style>
    
      CSS-in-JS made simple -- just Style It.
    </p>
    
    0 讨论(0)
提交回复
热议问题