Roadmap to becoming a developer in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

22 lines
493 B

import { useEffect, useState } from 'react';
export function useScrollPosition() {
const [scrollPosition, setScrollPosition] = useState<{
x: number;
y: number;
}>({
x: 0,
y: 0,
});
useEffect(() => {
const handleScroll = () => {
setScrollPosition({ x: window.scrollX, y: window.scrollY });
};
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, []);
return scrollPosition;
}