Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy 'scroll' event listener #40

Merged
merged 2 commits into from
Mar 2, 2023
Merged

Destroy 'scroll' event listener #40

merged 2 commits into from
Mar 2, 2023

Conversation

KalebKloppe
Copy link
Contributor

@KalebKloppe KalebKloppe commented Mar 1, 2023

Description

In debug mode, I noticed that the 'scroll' event listener was still firing, despite the ScrollyVideo component being removed.

Event Listener Not Destroyed

After some investigation, I found that this was because

  1. the scrollyVideo.destroy() method was never reached in the React component.
  2. 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.

const containerElement = useRef(null);
const [scrollyVideo, setScrollyVideo] = useState(null);

useEffect(() => {

  setScrollyVideo(new ScrollyVideo())

  return () => {
    if (scrollyVideo && scrollyVideo.destroy) scrollyVideo.destroy();
  }
}, [containerElement, props]);

I fixed this by using useRef instead of useState to hold the new ScrollyVideo.

const containerElement = useRef(null);
const scrollyVideoRef = useRef(null);

useEffect(() => {

  scrollyVideoRef.current = new ScrollyVideo()

  return () => {
    if (scrollyVideoRef.current && scrollyVideoRef.current.destroy) {
      scrollyVideo.destroy();
  }
}, [containerElement, props]);

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 wrapping this.updateScrollPercentage() prevented the removeEventListener from finding it.

window.addEventListener('scroll', () => this.updateScrollPercentage());
window.removeEventListener('scroll', this.updateScrollPercentage);

was changed to

window.addEventListener('scroll', this.updateScrollPercentage;
window.removeEventListener('scroll', this.updateScrollPercentage);

Other

These changes were tested in React and Vanilla JS but not Svelte, or Vue (as I don't know anything about them).

Copy link
Owner

@dkaoster dkaoster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you @KalebKloppe!

@dkaoster dkaoster merged commit e027213 into dkaoster:main Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants