Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
618 B · 20 lines
TypeScript
at main
123456789101112131415161718192021import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
/** * Returns current scroll top position in pixels. * * @returns {number | null} Scroll top position in pixels */export default function useScrollTop(): number | null { const [scrollTop, setScrollTop] = useState(isBrowser ? window.scrollY : null);
const getScrollTop = useCallback(() => setScrollTop(window.scrollY), []);
useEventListener(isBrowser ? document : null, 'scroll', getScrollTop);
return scrollTop;}