Here is the situation,
const MyComp = () => {
const timelineOverlayRef = useRef(null);
const [position, setPosition] = useState({ x: 0, y: 0 });
const mouseDownHandler = e => {
setPosition({ x: 0, y: 100});
};
const mouseMoveHandler = e => {
console.log(position);
}
const init = () => {
timelineOverlayRef.current.addEventListener('mousemove', mouseMoveHandler);
timelineOverlayRef.current.addEventListener('mousedown', mouseDownHandler);
}
useEffect(init, []);
return <div ref={timelineOverlayRef}>My Div where I want to capture mouse down and mouse move events</div>;
}
When I click on my div and start moving the mouse I don't see the updated position where y equals 100. instead I have the initial state value with y = 0. Any idea on why this might happen and how to overcome this will be really appreciated.