Blazor: when and how to cleanup JS Listeners / Observers / Element references? #54101
-
Hey guys, I discovered that calling JSInterop during disposal is not recommended: #45777. I also learned that cleaning up listeners, etc., during navigation is not advisable. Additionally, I found that using the mutation observer is recommended, so I spent hours trying to make it work. However, my results are always the same: if I have small components running some JS for animations or whatnot and I use the mutation observer, it is never called since the entire DOM tree is removed well above the component itself. This leaves me with the question: How am I supposed to correctly clean up JS references like listeners and observers when a component is being disposed of to avoid memory leaks? For example this JS code, that creates a listender and one observer and one mutation observer. the mutation observer never gets called because the component that uses the function is way down the element tree and never gets notified when an element is removed since one of the parents way obove this component is removed.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found my answer; apparently, it is fine to use IDisposable and the error only appears like in the mentioned thread. :D |
Beta Was this translation helpful? Give feedback.
I found my answer; apparently, it is fine to use IDisposable and the error only appears like in the mentioned thread. :D