Rehooks

useHover

Tracks the hover state of a specified element.

useHover

Installation

npx rehooks-cli@latest add 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

useHover.ts
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