Skip to content

Commit dd14833

Browse files
authored
Merge pull request #159 from streamich/fix-window-scroll
fix: 🐛 cancel animation frame on un-mount in useWindowScroll()
2 parents 94bffc5 + bc021ce commit dd14833

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/useWindowScroll.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const useWindowScroll = (): State => {
1616
useEffect(() => {
1717
const handler = () => {
1818
cancelAnimationFrame(frame.current)
19-
2019
frame.current = requestAnimationFrame(() => {
2120
setState({
2221
x: window.scrollX,
@@ -30,7 +29,10 @@ const useWindowScroll = (): State => {
3029
passive: true
3130
})
3231

33-
return () => window.removeEventListener('scroll', handler)
32+
return () => {
33+
cancelAnimationFrame(frame.current);
34+
window.removeEventListener('scroll', handler);
35+
};
3436
}, [])
3537

3638
return state

0 commit comments

Comments
 (0)