useOnHoverRef
About
A hook that returns a ref callback to easily attach hover event listeners (mouseenter and mouseleave) to DOM elements. The hook automatically manages event listeners and cleans them up when the component unmounts or the target element changes.
Examples
Basic example
Hover state management
Advanced example with event details
Arguments
Argument | Type | Description | Default |
---|---|---|---|
onMouseEnter | Function | Callback function called when mouse enters | undefined |
onMouseLeave | Function | Callback function called when mouse leaves | undefined |
Callback Parameters
Both onMouseEnter
and onMouseLeave
callbacks receive the following parameter:
Parameter | Type | Description |
---|---|---|
event | MouseEvent | The native mouse event object |
Returns
Return value | Type | Description |
---|---|---|
ref | Callback Ref | A ref callback that should be passed to the element you want to track |
Usage Notes
- The hook automatically handles adding and removing event listeners
- Event listeners are cleaned up when the component unmounts or when the target element changes
- Both callback parameters are optional - you can provide just one if needed
- The hook uses
mouseenter
andmouseleave
events, which don't bubble and only trigger when crossing the element boundary - Callbacks are wrapped with
useFreshCallback
to ensure they always have access to the latest values from closures