React useHover Hook
React useHover hook for hover state detection. Track mouse interactions with precise event handling and TypeScript support for Next.js applications.
Hover Detection Issues?
Join our Discord community for help from other developers.
Ever tried to track hover states in React and ended up with a mess of onMouseEnter/onMouseLeave event handlers scattered everywhere? You know the drill—manually managing boolean state, dealing with event bubbling issues, forgetting cleanup on unmount. This free open source React useHover custom hook handles all that hover detection boilerplate so you can focus on building interactive UIs instead of wrestling with mouse event logic in your React applications.
useHover showcase
Simple hover state detection with automatic event handling:
This free open source React hook simplifies hover interactions with TypeScript support for modern JavaScript applications. Whether you're building tooltips, button states, or interactive galleries in your Next.js projects, this React hook keeps your hover logic clean.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
npx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
Why most hover implementations suck
Look, you could keep writing onMouseEnter and onMouseLeave handlers for every element. But then you hit the edge cases—event bubbling, ref management, cleanup on unmount, TypeScript typing issues. Before you know it, you're copying the same hover logic across components.
Most developers scatter onMouseEnter/onMouseLeave handlers throughout their components without proper cleanup, causing memory leaks in React applications. Or they use mouseover/mouseout events that bubble unpredictably, triggering false hover states when child elements are involved. Some forget to handle component unmounting and leave orphaned event listeners in TypeScript components.
This React hook handles all the tricky parts—proper event delegation, ref validation, and automatic cleanup in Next.js projects. No more inline event handlers or forgotten mouseLeave events.
Plus it's just cleaner. Instead of managing hover state and event handlers manually, you pass a ref to the React hook and get back a boolean. Simple.
This free open source React hook handles the repetitive mouse event logic while you focus on building features. Whether you're creating React applications, Next.js interfaces, or TypeScript components, reliable hover detection keeps your JavaScript development smooth.
Features
- Hover state detection with mouseenter/mouseleave event handling in React applications
- Ref-based targeting allowing hover tracking on any DOM element in TypeScript components
- TypeScript support with generic element types and complete type definitions for Next.js projects
- Performance optimized using event delegation and proper cleanup in JavaScript development
- Zero dependencies lightweight implementation with just React hooks for modern frameworks
- Automatic cleanup handles event removal when component unmounts in React applications
- Free open source designed for modern React development workflows
When you'll actually use this
Real talk—this isn't for every hover interaction in React applications. CSS :hover works fine for simple style changes. But when you need hover state in your React component logic, this React hook keeps things organized in Next.js projects.
Perfect for:
- Interactive tooltips - Show/hide content based on hover state built with TypeScript
- Button feedback - Visual states and loading indicators on hover using React patterns
- Image galleries - Preview overlays and zoom triggers in JavaScript applications
- Navigation menus - Dropdown visibility and submenu logic in React components
- Card interfaces - Hover effects and action button reveals in Next.js applications
- Data visualization - Chart point highlighting and detail panels using TypeScript integration
API Reference
useHover
Parameter | Type | Description |
---|---|---|
elementRef | RefObject<T> | required - Ref object for the target DOM element |
Return Value
Type | Description |
---|---|
boolean | Current hover state - true when hovering, false otherwise |
Type Parameters
Parameter | Type | Default | Description |
---|---|---|---|
T | extends HTMLElement | HTMLElement | DOM element type for TypeScript inference |
Common gotchas
Valid ref requirement: The React hook returns false if the ref is null or undefined in TypeScript components, so make sure your ref is properly attached to a DOM element in React applications.
Event type selection matters: Uses mouseenter/mouseleave events that don't bubble like mouseover/mouseout in Next.js projects, which prevents issues with child elements triggering hover changes unexpectedly.
Automatic cleanup handling: Event listeners are removed when the component unmounts or the ref changes in JavaScript development, so you don't need to worry about memory leaks.
TypeScript generic benefits: The React hook accepts a generic type parameter for better type safety with specific HTML element types in TypeScript projects.
Mobile device limitations: Mobile devices don't have hover states in React applications. The hook always returns false on touch devices, so consider alternative interactions for mobile users.
Element visibility requirements: Zero-height or hidden elements can't be hovered in Next.js applications. Make sure your target elements have visible dimensions.
Related hooks you will also like
useEventListener
Custom event listener management that useHover is built upon
useBoolean
Boolean state management with toggle helpers for hover states
useClickAnyWhere
Global click detection for mouse interaction patterns
useOnClickOutside
Detect clicks outside specific elements for tooltip dismissal
useTimeout
Add delays to hover interactions for better UX
useMousePosition
Track mouse cursor position for advanced hover effects
Questions you might have
React useEventListener Hook
React useEventListener hook for DOM event listeners. Automatic cleanup and TypeScript safety with JavaScript integration for Next.js apps.
React IntersectionObserver Hook
React useIntersectionObserver hook for element visibility detection. Track viewport intersections with configurable thresholds for Next.js apps.