简体   繁体   中英

remove event listener in reactJS

I wants to remove event listener that are already in event listener.My Code is

  public componentDidMount() {
this.drags();
}
private drags(){
 const e = ReactDOM.findDOMNode(this.container);
    if (e) {
      e.addEventListener("mousedown", (event: any) => {
      ....
       parent = ReactDOM.findDOMNode(this).parentNode;
       if (parent) {
         parent.addEventListener("mousemove", (event1: any) => {
         ....
         const eDrag = parent.getElementsByClassName("draggable");
          eRes[0].addEventListener("mouseup", (event3: any) => {
          **// HERE I WANT TO REMOVE LISTENER OF PARENT OF MOUSE MOVE**
          }
        }
       }

    }
  }

}

Can anybody help me in this ?

Do not use anonymous function as the event handler, use a named function instead.

So, if you add the listener this way:

function doSomething() {
  // something 
}

window.addEventListener('mousedown', this.doSomething);

You can remove it like:

window.removeEventListener('mousedown', this.doSomething);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM