2019-07-02 06:39:17 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
export default function useHover(ref) {
|
|
|
|
const [isHovering, setIsHovering] = useState(false);
|
|
|
|
|
2020-04-23 18:31:42 +02:00
|
|
|
const hoverFunc = () => setIsHovering(true);
|
|
|
|
const unHoverFunc = () => setIsHovering(false);
|
2019-07-02 06:39:17 +02:00
|
|
|
|
2020-04-23 18:31:42 +02:00
|
|
|
useEffect(() => {
|
2019-07-02 06:39:17 +02:00
|
|
|
const refElement = ref.current;
|
|
|
|
if (refElement) {
|
2020-04-23 18:31:42 +02:00
|
|
|
refElement.addEventListener('mouseenter', hoverFunc);
|
|
|
|
refElement.addEventListener('mouseleave', unHoverFunc);
|
|
|
|
|
2019-07-02 06:39:17 +02:00
|
|
|
return () => {
|
2020-04-23 18:31:42 +02:00
|
|
|
refElement.removeEventListener('mouseenter', hoverFunc);
|
|
|
|
refElement.removeEventListener('mouseleave', unHoverFunc);
|
2019-07-02 06:39:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [ref, isHovering]);
|
|
|
|
|
|
|
|
return isHovering;
|
|
|
|
}
|