Skip to content

Comments

fix(useScrolling): clear timeout on unmount to prevent memory leak#2698

Open
selvaganapathycoder wants to merge 1 commit intostreamich:masterfrom
selvaganapathycoder:fix/useScrolling-cleanup-timeout
Open

fix(useScrolling): clear timeout on unmount to prevent memory leak#2698
selvaganapathycoder wants to merge 1 commit intostreamich:masterfrom
selvaganapathycoder:fix/useScrolling-cleanup-timeout

Conversation

@selvaganapathycoder
Copy link

Summary

The useEffect cleanup function in useScrolling was not clearing the scrollingTimeout. This could lead to:

  • A memory leak from the retained timeout reference
  • A setState call on an unmounted component, causing a React warning

Changes

Added clearTimeout(scrollingTimeout) to the useEffect cleanup/return function so that the timeout is properly cleared when the component unmounts or the effect re-runs.

Before

return () => {
  if (ref.current) {
    off(ref.current, 'scroll', handleScroll, false);
  }
};    

after


return () => {
  clearTimeout(scrollingTimeout);
  if (ref.current) {
    off(ref.current, 'scroll', handleScroll, false);
  }
};

The useEffect cleanup function in useScrolling was not clearing the scrollingTimeout, which could cause a memory leak and a setState call on an unmounted component. Added clearTimeout(scrollingTimeout) to the cleanup function.
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.

1 participant