Skip to content

Commit

Permalink
fix: πŸ› cancel animation frame on un-mount in useWindowScroll()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 23, 2019
1 parent 94bffc5 commit bc021ce
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/useWindowScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const useWindowScroll = (): State => {
useEffect(() => {
const handler = () => {
cancelAnimationFrame(frame.current)

frame.current = requestAnimationFrame(() => {
setState({
x: window.scrollX,
Expand All @@ -30,7 +29,10 @@ const useWindowScroll = (): State => {
passive: true
})

return () => window.removeEventListener('scroll', handler)
return () => {
cancelAnimationFrame(frame.current);
window.removeEventListener('scroll', handler);
};
}, [])

return state
Expand Down

0 comments on commit bc021ce

Please sign in to comment.