Skip to main content
Version: 3.x

useMouseMove

This hook provides a way to handle mouse move event on a HTMLElement or window.

Arguments

callback [ function ]

First argument is a callback function with an event object as its first argument which is called on every mouse move.

Here are the properties of an event object argument of a callback function:

PropertyDescription
targetevent.target element the mouse move is hovered upon.
isMovingBoolean indicating the current status of mouse movement.
mouseXHorizontal mouse movement amount.
mouseYVertical mouse movement amount.
velocityXVelocity along horizontal mouse movement direction.
velocityYVelocity along vertical mouse movement direction.
directionXIndicates the current horizontal mouse movement direction. For positive +1, for negative -1 and for not moving 0.
directionYIndicates the current vertical mouse movement direction. For positive +1, for negative -1 and for not moving 0.

Returns

It returns a function which can be spread on any HTMLELement. If not bound to any HTMLElement, event is attached to window.

Define a hook for window:

useMouseMove((event) => doSomething(event));

Or you could bind it in any HTMLElement:

const bind = useMouseMove((event) => doSomething(event));

Apply it on a HTMLELement:

<div {...bind()}>...</div>

Example

In the below example, useMouseMove hook is used to get mouseX and mouseY.

import { useMouseMove } from "react-ui-animate";

export default function() {

const bind = useMouseMove(
function ({ mouseX, mouseY }) {
console.log("XY", mouseX, mouseY);
},
);

return (...);
}