2019-07-02 06:39:17 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
export default function useHover(ref) {
|
|
|
|
const [isHovering, setIsHovering] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function handleHover() {
|
|
|
|
setIsHovering(!isHovering);
|
|
|
|
}
|
|
|
|
|
|
|
|
const refElement = ref.current;
|
|
|
|
if (refElement) {
|
2019-07-17 22:49:06 +02:00
|
|
|
refElement.addEventListener('mouseenter', handleHover);
|
2019-07-02 06:39:17 +02:00
|
|
|
refElement.addEventListener('mouseleave', handleHover);
|
|
|
|
return () => {
|
2019-07-17 22:49:06 +02:00
|
|
|
refElement.removeEventListener('mouseenter', handleHover);
|
2019-07-02 06:39:17 +02:00
|
|
|
refElement.removeEventListener('mouseleave', handleHover);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [ref, isHovering]);
|
|
|
|
|
|
|
|
return isHovering;
|
|
|
|
}
|