madiator.com/ui/effects/use-hover.js
2021-08-16 12:11:25 +02:00

23 lines
658 B
JavaScript

import { useEffect, useState } from 'react';
export default function useHover(ref) {
const [isHovering, setIsHovering] = useState(false);
const hoverFunc = () => setIsHovering(true);
const unHoverFunc = () => setIsHovering(false);
useEffect(() => {
const refElement = ref.current;
if (refElement) {
refElement.addEventListener('mouseenter', hoverFunc);
refElement.addEventListener('mouseleave', unHoverFunc);
return () => {
refElement.removeEventListener('mouseenter', hoverFunc);
refElement.removeEventListener('mouseleave', unHoverFunc);
};
}
}, [ref, isHovering]);
return isHovering;
}