0
const ControlPanel = ({ mainSectionRef }) => {
    const [canMove, setCanMove] = useState(false)
    const classes = useStyles();
    const toolbarRef = useRef();

    const onMouseMove = function (e) {
        const { x, y } = getMouseCoordinatesOnCanvas(e, mainSectionRef.current);
        toolbarRef.current.style.left = x +'px';
        toolbarRef.current.style.top = y + 'px';
    }

    const setCan = () => {
        setCanMove(!canMove)
    }

    if (mainSectionRef.current) {
        //issue is here!!!!
        mainSectionRef.current.removeEventListener('mousemove', onMouseMove, true)
        if (canMove) {
            mainSectionRef.current.addEventListener('mousemove', onMouseMove, true)
        }
    }

    return (
        <Toolbar ref={toolbarRef}>
           <RadioButtonUncheckedIcon />
           <CropSquareIcon />
           <ArrowRightAltIcon />
           <CloseIcon />
           <DragIndicatorIcon style={{ backgroundColor: canMove ? "red" : "inherit" }} onClick={setCan}/>
        </Toolbar >
    );
}
export default ControlPanel;

I'm trying to remove the event listener whenever there is a state change in the component. mainSectionref is a parent div and the control panel is the child component. I'm trying to move the control panel in the mainSection onClick of a button. The event does get added but on state change, it doesn't seem to remove the event listener. Can I know what's going wrong?.

2 Answers 2

1

You should use useEffect hook from react and put your addEventListener and removeEventListener code inside useEffect as first argument. useEffect takes second argument as dependency it means on what values change useEffect should call . In your case you want to call useEffect when ever canMove changes.

  useEffect(() => {
   if (mainSectionRef.current) {
   mainSectionRef.current.removeEventListener('mousemove', onMouseMove, true)
     if (canMove) {
     mainSectionRef.current.addEventListener('mousemove', onMouseMove, true)
    }
   }
  }, [canMove]);

also remember : import React, { useState, useEffect } from 'react';

Sign up to request clarification or add additional context in comments.

1 Comment

Hey Zohaib, thanks for the response. I appreciate the enhancement. Unfortunately, the problem still exists. The removeEventListener seems to be not working for some reason. It would be great if somebody could identify the issue. Thanks, zohaib for the suggestion though.
0
useEffect(() => {
    function onMouseMove(e) {
        //Some code
    }
    if (mainSectionRef.current) {
        **mainSectionRef.current.onmousemove = null**;
        if (canMove) {
            mainSectionRef.current.onmousemove = e => onMouseMove(e);
        }
    }
}, [canMove])

The issue is resolved by using the key onmousemove. I still don't know why removeEventListener is not working in the above case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.