In debug mode, I noticed that the 'scroll' event listener was still firing, despite the ScrollyVideo component being removed.
After some investigation, I found that this was because
the scrollyVideo.destroy() method was never reached in the React component.
the anonymous function wrapping this.updateScrollPercentage in the addEventListener prevented removeEventListener from destroying it.
React Component
(scrollyVideo && scrollyVideo.destroy) always evaluated false and the scrollyVideo.destroy() method couldn't be reached. setScrollyVideo is called inside the useEffect hook, and it may not be set when the cleanup function is called.
Once destroy() could be called, I noticed the event listener was still not being destroyed. After further investigation, I found the anonymous function wrapping this.updateScrollPercentage() prevented the removeEventListener from finding it.
Description
In debug mode, I noticed that the 'scroll' event listener was still firing, despite the ScrollyVideo component being removed.
After some investigation, I found that this was because
scrollyVideo.destroy()
method was never reached in the React component.this.updateScrollPercentage
in theaddEventListener
preventedremoveEventListener
from destroying it.React Component
(scrollyVideo && scrollyVideo.destroy)
always evaluated false and thescrollyVideo.destroy()
method couldn't be reached.setScrollyVideo
is called inside theuseEffect
hook, and it may not be set when the cleanup function is called.I fixed this by using
useRef
instead ofuseState
to hold thenew ScrollyVideo
.Event Listener
Once
destroy()
could be called, I noticed the event listener was still not being destroyed. After further investigation, I found the anonymous function wrappingthis.updateScrollPercentage()
prevented theremoveEventListener
from finding it.was changed to
Other
These changes were tested in React and Vanilla JS but not Svelte, or Vue (as I don't know anything about them).