useHover

Detect if an element is being hovered.

https://www.shaktools.com/shak-hooks
Sensor

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useHover } from "@shak-hooks/usehooks";
Source: packages/react/src/useHover.ts

Signature

signature.ts
export function useHover<T extends HTMLElement = HTMLElement>(elementRef: RefObject<T>): boolean

Parameters

NameTypeOptionalDefault
elementRefRefObject<T>No-

Returns

boolean

Implementation

use-hover.ts
1import { useState, useRef, useEffect } from "react";
2
3export function useHover<T extends HTMLElement = HTMLElement>() {
4 const [value, setValue] = useState(false);
5 const ref = useRef<T>(null);
6
7 const handleMouseOver = () => setValue(true);
8 const handleMouseOut = () => setValue(false);
9
10 useEffect(() => {
11 const node = ref.current;
12 if (node) {
13 node.addEventListener("mouseover", handleMouseOver);
14 node.addEventListener("mouseout", handleMouseOut);
15
16 return () => {
17 node.removeEventListener("mouseover", handleMouseOver);
18 node.removeEventListener("mouseout", handleMouseOut);
19 };
20 }
21 }, [ref.current]);
22
23 return [ref, value] as const;
24}

Advertisement

Google Ads

Usage

example.tsx
1import { useHover } from "@shak-hooks/usehooks";
2
3const result = useHover(undefined);
4// result: boolean
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy