Web component gotcha: constructor vs connectedCallback
A common mistake I see in web components is this:
class MyComponent extends HTMLElement {
constructor() {
super()
setupLogic()
}
disconnectedCallback() {
teardownLogic()
}
}
This setupLogic() can be just about anything – subscribing to a store, setting up event listeners, etc. The teardownLogic() is designed to undo those things – unsubscribe from a store, remove event listeners, etc.
The problem is that constructor is called once, when the component is created. Whereas disconnectedCallback can ...
Read more at nolanlawson.com