fix: 🐛 cancel animation frame on un-mount in useWindowScroll()

This commit is contained in:
streamich 2019-03-23 08:40:31 +01:00
parent 94bffc514d
commit bc021ce550

View File

@ -16,7 +16,6 @@ const useWindowScroll = (): State => {
useEffect(() => {
const handler = () => {
cancelAnimationFrame(frame.current)
frame.current = requestAnimationFrame(() => {
setState({
x: window.scrollX,
@ -30,7 +29,10 @@ const useWindowScroll = (): State => {
passive: true
})
return () => window.removeEventListener('scroll', handler)
return () => {
cancelAnimationFrame(frame.current);
window.removeEventListener('scroll', handler);
};
}, [])
return state