mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
fix: add additional ref check on clean up
This commit is contained in:
parent
b3ba702ef8
commit
d18d2d8683
@ -3,27 +3,28 @@ import { RefObject, useEffect, useState } from 'react';
|
||||
const useScrolling = (ref: RefObject<HTMLElement>): boolean => {
|
||||
const [scrolling, setScrolling] = useState<boolean>(false);
|
||||
|
||||
useEffect(
|
||||
() => {
|
||||
if (ref.current) {
|
||||
let scrollingTimeout;
|
||||
useEffect(() => {
|
||||
if (ref.current) {
|
||||
let scrollingTimeout;
|
||||
|
||||
const handleScrollEnd = () => {
|
||||
setScrolling(false);
|
||||
};
|
||||
const handleScrollEnd = () => {
|
||||
setScrolling(false);
|
||||
};
|
||||
|
||||
const handleScroll = () => {
|
||||
setScrolling(true);
|
||||
clearTimeout(scrollingTimeout);
|
||||
scrollingTimeout = setTimeout(() => handleScrollEnd(), 150);
|
||||
};
|
||||
const handleScroll = () => {
|
||||
setScrolling(true);
|
||||
clearTimeout(scrollingTimeout);
|
||||
scrollingTimeout = setTimeout(() => handleScrollEnd(), 150);
|
||||
};
|
||||
|
||||
ref.current.addEventListener('scroll', handleScroll, false);
|
||||
return () => ref.current.removeEventListener('scroll', handleScroll, false);
|
||||
}
|
||||
},
|
||||
[ref.current],
|
||||
);
|
||||
ref.current.addEventListener('scroll', handleScroll, false);
|
||||
return () => {
|
||||
if (ref.current) {
|
||||
ref.current.removeEventListener('scroll', handleScroll, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
}, [ref.current]);
|
||||
|
||||
return scrolling;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user