import { useEffect } from 'react' ; param { next const useEscToNavigateBack = ( router ) => { const handleEsc = ( event ) => { if ( event . keyCode === 27 ) { const oldPath = router . asPath ; router . back ( ) ; const newPath = router . asPath ; if ( oldPath === newPath ) { router . push ( '/' ) ; } } } ; useEffect ( ( ) => { window . addEventListener ( 'keydown' , handleEsc ) ; return ( ) => { window . removeEventListener ( 'keydown' , handleEsc ) ; } ; } , [ ] ) ; } ; export default useEscToNavigateBack ;