use-page-offset/src/usePageOffset.js
/**
 * Custom hook to track windows Y offset
 *
 * const offset = usePageOffset()
 * @returns number
 */
Custom hook to track windows Y offset
const offset = usePageOffset()
@returns number
Custom hook to track windows Y offset const offset = usePageOffset () @returns number
function usePageOffset() {
    const [offset, setOffset] = useState(0)
    const handleScroll = (pageYOffset) => {
        setOffset(pageYOffset)
    }
    useEffect(() => {
        document.addEventListener('scroll', () => {
            handleScroll(window.pageYOffset)
        })
        return () => {
            document.removeEventListener('scroll', () => {
                handleScroll(window.pageYOffset)
            })
        }
    }, [])
    return offset
}
function usePageOffset ( ) { const [ offset , setOffset ] = useState ( 0 ) const handleScroll = ( pageYOffset ) => { setOffset ( pageYOffset ) } useEffect ( ( ) => { document . addEventListener ( 'scroll' , ( ) => { handleScroll ( window . pageYOffset ) } ) return ( ) => { document . removeEventListener ( 'scroll' , ( ) => { handleScroll ( window . pageYOffset ) } ) } } , [ ] ) return offset }
Custom hook to track windows Y offset
 const offset = usePageOffset()
 @returns number
Custom hook to track windows Y offset const offset = usePageOffset () @returns number
/**
     * Define local state
     */ /**
     * Sets pageYOffset in state
     * @param pageYOffset number
     */ /**
     * Adds event listener for scroll and removes on unmount
     */ /**
     * Returns pageYoffset
     */