Created
July 14, 2021 23:00
-
-
Save marcovincit/095c905baf7bd3818fb3f00df57a5459 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { useEffect, useState } from "react"; | |
export default function useCursorPosition() { | |
const [cursorPositionX, setCursorPositionX] = useState(0); | |
const [cursorPositionY, setCursorPositionY] = useState(0); | |
useEffect(() => { | |
function updateCursorPosition(e) { | |
setCursorPositionX(e !== undefined ? e.clientX : 0); | |
setCursorPositionY(e !== undefined ? e.clientY : 0); | |
} | |
window.addEventListener("mousemove", updateCursorPosition); | |
updateCursorPosition(); | |
return () => window.removeEventListener("mousemove", updateCursorPosition); | |
}, []); | |
return { cursorPositionX, cursorPositionY }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment