skip to Main Content

Reactjs – using useRef instead of a setState state variable in a React component when registering eventListeners with a handler that refers to that variable

const movingRef = useRef(false); const handleStartMove = (e) => { movingRef.current = true; e.stopPropagation(); // handleMove doesn't take the latest value if we use a state instead of a ref document.addEventListener('mousemove', handleMove); if (onMoveStart) { onMoveStart(e, movingRef.current, angle); } };…

VIEW QUESTION
Back To Top
Search