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

前端 未结 13 2251
旧时难觅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 }) => (
        
    {children}
    {onHover}
    )

    Usage

    Then use it like this:

         Show this on hover 
    }>
    Show on no hover

提交回复
热议问题