import * as React from 'react' ; const { useState , useMemo , useEffect } = React ; export const useScroll = ( ) => { const [ x , setX ] = useState ( typeof window === 'undefined' ? 0 : window . scrollX ) ; const [ y , setY ] = useState ( typeof window === 'undefined' ? 0 : window . scrollY ) ; const handler = useMemo ( ( ) => ( ) => { setX ( window . scrollX ) ; setY ( window . scrollY ) ; } , [ ] ) ; useEffect ( ( ) => { if ( typeof window !== 'undefined' ) { window . addEventListener ( 'scroll' , handler ) ; } return ( ) => { if ( typeof window !== 'undefined' ) { window . removeEventListener ( 'scroll' , handler ) ; } } ; } ) ; return [ x , y ] ; } ;