Skip to content

Commit d494523

Browse files
fix(Navigation): fix issue with remaining event handler in effect (#220)
1 parent b7b729f commit d494523

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/navigation/components/Navigation/Navigation.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {NavigationListItem} from '../NavigationListItem/NavigationListItem';
1111
import './Navigation.scss';
1212

1313
const b = block('navigation');
14+
const EVENT_HANDLE_DELAY = 100;
1415

1516
export interface NavigationProps {
1617
links: NavigationItemModel[];
@@ -48,25 +49,28 @@ const Navigation: React.FC<NavigationProps> = ({
4849
}, []);
4950

5051
useEffect(() => {
51-
const debouncedCalculateItemPositions = _.debounce(calculateItemPositions, 100);
52-
const calculateOnScroll = _.debounce(() => {
52+
const debouncedCalculateItemPositions = _.debounce(
53+
calculateItemPositions,
54+
EVENT_HANDLE_DELAY,
55+
);
56+
const debouncedCalculateOnScroll = _.debounce(() => {
5357
const curLeftScroll = window.pageXOffset;
5458

5559
if (curLeftScroll !== lastLeftScroll) {
5660
setLastLeftScroll(window.pageXOffset);
5761
calculateItemPositions();
5862
}
59-
}, 100);
63+
}, EVENT_HANDLE_DELAY);
6064

6165
calculateItemPositions();
6266
setLastLeftScroll(window.pageXOffset);
6367

6468
window.addEventListener('resize', debouncedCalculateItemPositions);
65-
window.addEventListener('scroll', calculateOnScroll);
69+
window.addEventListener('scroll', debouncedCalculateOnScroll);
6670

6771
return () => {
68-
window.removeEventListener(`resize`, calculateItemPositions);
69-
window.removeEventListener('scroll', calculateOnScroll);
72+
window.removeEventListener(`resize`, debouncedCalculateItemPositions);
73+
window.removeEventListener('scroll', debouncedCalculateOnScroll);
7074
};
7175
}, [calculateItemPositions, itemRefs, lastLeftScroll]);
7276

0 commit comments

Comments
 (0)