Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created March 25, 2021 23:36
Show Gist options
  • Save johnnyferreiradev/69c32bbc1987e7ae85383d0bf596ba97 to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/69c32bbc1987e7ae85383d0bf596ba97 to your computer and use it in GitHub Desktop.
Hook para aplicar o evento mouseover nos componentes react
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