Rehooks
Content

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(): boolean;

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

Returns

NameTypeDescription
isHoveringbooleanA boolean value indicating whether the user is hovering over the element.

On this page