Event listeners should be cleaned up once a component has been removed from DOM to prevent memory leaks.
This rule requires that listeners added in connectedCallback
are removed
in disconnectedCallback
.
The following patterns are considered warnings:
class Foo extends HTMLElement {
connectedCallback() {
this.addEventListener('click', this.foo);
}
}
The following patterns are not warnings:
class Foo extends HTMLElement {
connectedCallback() {
this.addEventListener('click', this.foo);
}
disconnectedCallback() {
this.removeEventListener('click', this.foo);
}
}
If you don't care about teardown of listeners, do not use this rule.