Created
March 25, 2021 23:36
-
-
Save johnnyferreiradev/69c32bbc1987e7ae85383d0bf596ba97 to your computer and use it in GitHub Desktop.
Hook para aplicar o evento mouseover nos componentes react
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react'; | |
const useOutsideEvent = (ref, onAction) => { | |
useEffect(() => { | |
function handleClickOutside(event) { | |
if (ref.current && !ref.current.contains(event.target)) { | |
onAction(); | |
} | |
} | |
document.addEventListener('mousedown', handleClickOutside); | |
return () => { | |
document.removeEventListener('mousedown', handleClickOutside); | |
}; | |
}, [ref]); | |
}; | |
export default useOutsideEvent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment