N
N
Nick Fisher2020-02-19 22:02:58
JavaScript
Nick Fisher, 2020-02-19 22:02:58

How to remove eventListener from window?

Hello. I have a screen form where a table and filters are rendered. In filters (in the filter hock didmount, to be more precise), I have listeners that listen to enter (keyup), then a pop-up appears on top of the table and filter, where the same filter component is used and it hangs the listener again. It turns out that when you click on enter, events occur both in the pop-up and in the table, because the table itself is not unmountable. Question: are there ways to somehow remove a specific lisener, given that the function that is bound cannot be anonymous (i.e. the link is always different, it will not work directly)? If anything - e.stopPropagation doesn't help either. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2020-02-19
@Stalker_RED

removeEventListener , but it needs a function reference.

A
Aetae, 2020-02-19
@Aetae

If it does not help e.stopPropagation- you can zayuzat e.stopImmediatePropagation. :)
And it even works. In my own way.
You can also hang handlers through a custom wrapper over addEventListener, where you can keep track of all hung handlers in relation to the element (or, for example, to a manually set label) and, accordingly, safely remove them without knowing the function.
Only here the problem is in your architecture.
Find the reason why the same handlers are hung twice and/or do not take into account focus/context switches and fix it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question