Mouse Position Hook
// hooks/use-mouse-position.js
import React from "react";
function useMousePosition() {
const [mousePosition, setMousePosition] = React.useState({
x: 0,
y: 0,
});
React.useEffect(() => {
function handleMouseMove(event) {
setMousePosition({
x: event.clientX,
y: event.clientY,
});
}
window.addEventListener("mousemove", handleMouseMove);
return () => {
window.removeEventListener("mousemove", handleMouseMove);
};
}, []);
return mousePosition;
}
export default useMousePosition;