Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No overload matches this call" for removeEventListener and addEventListener

I have a keydown handler function defined like so:

 handleOnKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {
    if (key.isEscape(e.key)) {
      stopEvent(e);
      this.props.handleClose(e);
    }
  }

However in another part of my code where this function is passed to removeEventListener and addEventListener:

componentDidMount () {
    const { current: modal } = this.modalRef;
    if (modal) {
      modal.addEventListener('keydown', this.handleOnKeyDown);
    }
  }

  componentWillUnmount () {
    const { current: modal } = this.modalRef;
    if (modal) {
      modal.removeEventListener('keydown', this.handleOnKeyDown);
    }
  }

I get the following Typescript error:

No overload matches this call.
  Overload 1 of 2, '(type: "keydown", listener: (this: HTMLDivElement, ev: KeyboardEvent) => any, options?: boolean | EventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: React.KeyboardEvent<HTMLButtonElement>) => void' is not assignable to parameter of type '(this: HTMLDivElement, ev: KeyboardEvent) => any'.
      Types of parameters 'e' and 'ev' are incompatible.
        Type 'KeyboardEvent' is missing the following properties from type 'KeyboardEvent<HTMLButtonElement>': locale, nativeEvent, isDefaultPrevented, isPropagationStopped, persist
  Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions | undefined): void', gave the following error.
    Argument of type '(e: React.KeyboardEvent<HTMLButtonElement>) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
      Type '(e: React.KeyboardEvent<HTMLButtonElement>) => void' is not assignable to type 'EventListener'.
        Types of parameters 'e' and 'evt' are incompatible.
          Type 'Event' is missing the following properties from type 'KeyboardEvent<HTMLButtonElement>': altKey, charCode, ctrlKey, getModifierState, and 12 more.ts(2769)

What would be work around for this?

like image 958
cclerv Avatar asked Jan 09 '20 23:01

cclerv


People also ask

How can I call addEventListener only once?

Using the once option We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is achieved by passing the property once to the object. If we set once to true, the event will only be fired once.

What is false in addEventListener?

addEventListener("click", first, true); when clicking child element, first method will be called before second . By default, the useCapture flag is set to false which means you handler will only be called during event bubbling phase.

What is addEventListener click?

addEventListener("click", displayDate); Try it Yourself » The addEventListener() method attaches an event handler to the specified element. The addEventListener() method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element.

What is the use of removeeventlistener?

Definition and Usage The removeEventListener () method removes an event handler that has been attached with the addEventListener () method. Note: To remove event handlers, the function specified with the addEventListener () method must be an external function, like in the example above (myFunction).

How to remove an event listener previously added by another event?

Given an event listener previously added by calling addEventListener (), you may eventually come to a point at which you need to remove it. Obviously, you need to specify the same type and listener parameters to removeEventListener (). But what about the options or useCapture parameters?

What is the difference between removeeventlistener () and useCapture ()?

listener: It is the function of the event handler to remove. useCapture: It is an optional parameter. By default it is Boolean value false which specifies the removal of event handler from the bubbling phase and if it is true than the removeEventListener () method removes the event handler from the capturing phase. "mouseover Event !!" + "<br>";

How do I remove a MouseMove event from addEventListener?

Remove a "mousemove" event that has been attached with the addEventListener () method: The removeEventListener () method removes an event handler that has been attached with the addEventListener () method.


Video Answer


1 Answers

The issue appears to be answered this question: react typescript issue on addEvenListener

The handler function for addEventListener and removeEventListener does not accept a React.KeyboardEvent as a parameter. It needs a DOM event.

like image 116
mark Avatar answered Nov 04 '22 19:11

mark