Created
July 26, 2022 06:26
-
-
Save evolify/d6ffaae840f0755f72a4e6aea8502a7d to your computer and use it in GitHub Desktop.
useMousePosition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from "react"; | |
export default function useMousePosition() { | |
const [mousePosition, setMousePosition] = useState({ x: null, y: null }); | |
useEffect(() => { | |
const updateMousePosition = (ev) => { | |
setMousePosition({ x: ev.clientX, y: ev.clientY }); | |
}; | |
window.addEventListener("mousemove", updateMousePosition); | |
return () => { | |
window.removeEventListener("mousemove", updateMousePosition); | |
}; | |
}, []); | |
return mousePosition; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment