react-use
react-use copied to clipboard
fix(useScrolling): clear timeout on unmount to prevent memory leak
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);
}
};