I'm studying react, but there's something I can't understand. Where does this code get the mouse position value?
function useMousePosition() {
const [x, setX] = useState(0);
const [y, setY] = useState(0);
const setPosition = ({ x, y }) => {
setX(x);
setY(y);
};
useLayoutEffect(() => {
window.addEventListener("mousemove", setPosition);
return () => window.removeEventListener("mousemove", setPosition);
}, []);
return [x, y];
}
It is here:
window.addEventListener("mousemove", setPosition);
and here:
const setPosition = ({ x, y }) => {
setX(x);
setY(y);
};
The first part listens for mouse movement, and when it happens, passes the data to the x and y variables of the setPosition function, the second part.