useHover

Handles pointer hover interactions for an element. Normalizes behavior across browsers and platforms, and ignores emulated mouse events on touch devices.

Hover me!
    import React from 'react';
    import {useHover} from 'react-aria/useHover';
    
    function Example() {
      let [events, setEvents] = React.useState<string[]>([]);
      let {hoverProps, isHovered} = useHover({
        onHoverStart: e => setEvents(
          events => [...events, `hover start with ${e.pointerType}`]
        ),
        onHoverEnd: e => setEvents(
          events => [...events, `hover end with ${e.pointerType}`]
        )
      });
    
      return (
        <>
          <div
            {...hoverProps}
            style={{
              background: isHovered ? 'darkgreen' : 'green',
              color: 'white',
              display: 'inline-block',
              padding: '8px 12px',
              borderRadius: 8,
              cursor: 'pointer'
            }}
            role="button"
            tabIndex={0}>
            Hover me!
          </div>
          <ul
            style={{
              maxHeight: '200px',
              overflow: 'auto'
            }}>
            {events.map((e, i) => <li key={i}>{e}</li>)}
          </ul>
        </>
      );
    }
    

    Features

    useHover is similar to the :hover CSS pseudo class, but only applies on mouse interactions. :hover is sticky on touch devices, applying continuously until the user interacts with another element, and on devices with both mouse and touch support there is no CSS-only way to apply hover states only when interacting with a pointer. Read our blog post to learn more.

    API

    useHover(props: ):

    HoverProps

    NameType
    isDisabledboolean

    Whether the hover events should be disabled.

    HoverResult

    NameType
    hoverPropsDOMAttributes

    Props to spread on the target element.

    isHoveredboolean

    HoverEvent

    NameType
    type'hoverstart' | 'hoverend'

    The type of hover event being fired.

    pointerType'mouse' | 'pen'

    The pointer type that triggered the hover event.