useHover
function useHover(ref: RefObject<Element | null>): boolean;
Returns whether a mouse or pen pointer is over the element, including over its children.
- Touch is ignored: touch screens have no hover, and a tap would otherwise leave it
true. - It is
falseduring server rendering and when the pointer leaves the window. - It checks the ref's current element as the pointer moves, so it follows the ref to a new element.
For purely visual effects, CSS :hover is simpler. Use this when hovering changes what
renders, for example to load a preview.
Live demo
Free shipping
Orders over $50 ship free.
Example
import { useRef } from "react";
import { useHover } from "react-honey-hooks";
function UserLink({ user }: { user: User }) {
const ref = useRef<HTMLAnchorElement>(null);
const hovered = useHover(ref);
return (
<a ref={ref} href={user.url}>
{user.name}
{hovered && <ProfilePreview userId={user.id} />}
</a>
);
}