import * as React from 'react' ; const { useRef , useEffect } = React ; export default function useEventListener ( eventName , handler , element = window , config ? ) { const saveHandlerRef = useRef < any > ( ) ; useEffect ( ( ) => { saveHandlerRef . current = handler ; } , [ handler ] ) ; useEffect ( ( ) => { const isSupported = element && element . addEventListener ; if ( ! isSupported ) return ; const eventListener = ( event ) => saveHandlerRef . current ( event ) ; element . addEventListener ( eventName , eventListener , config ) ; return ( ) => { element . removeEventListener ( eventName , eventListener , config ) ; } ; } , [ eventName , element ] ) ; }