Rehooks
Hooks

useHover

A hook that returns a boolean value indicating whether the user is hovering over an element.

useHover

Usage

Component.tsx
import { useHover } from "~/hooks/useHover";
 
export default function Component() {
  const [ref, isHovered] = useHover<HTMLDivElement>();
 
  return (
    <div
      ref={ref}
      style={{
        width: "200px",
        height: "200px",
        backgroundColor: isHovered ? "lightblue" : "lightcoral",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        transition: "background-color 0.3s",
      }}
    >
      <p>{isHovered ? "Hovered!" : "Hover over me!"}</p>
    </div>
  );
}

API

useHover

function useHover<T extends HTMLElement>(): [Ref<T>, boolean];

Returns a boolean value indicating whether the user is hovering over the element.

Parameters

NameTypeDescription
TTThe type of the element to track.

Returns

NameTypeDescription
refRefA ref to the element to track.
isHoveringbooleanA boolean value indicating whether the user is hovering over the element.

On this page