fix: better hover use-effect
This commit is contained in:
parent
3c6469f388
commit
4efc0fbd3e
1 changed files with 8 additions and 8 deletions
|
@ -3,18 +3,18 @@ import { useEffect, useState } from 'react';
|
||||||
export default function useHover(ref) {
|
export default function useHover(ref) {
|
||||||
const [isHovering, setIsHovering] = useState(false);
|
const [isHovering, setIsHovering] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const hoverFunc = () => setIsHovering(true);
|
||||||
function handleHover() {
|
const unHoverFunc = () => setIsHovering(false);
|
||||||
setIsHovering(!isHovering);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const refElement = ref.current;
|
const refElement = ref.current;
|
||||||
if (refElement) {
|
if (refElement) {
|
||||||
refElement.addEventListener('mouseenter', handleHover);
|
refElement.addEventListener('mouseenter', hoverFunc);
|
||||||
refElement.addEventListener('mouseleave', handleHover);
|
refElement.addEventListener('mouseleave', unHoverFunc);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
refElement.removeEventListener('mouseenter', handleHover);
|
refElement.removeEventListener('mouseenter', hoverFunc);
|
||||||
refElement.removeEventListener('mouseleave', handleHover);
|
refElement.removeEventListener('mouseleave', unHoverFunc);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [ref, isHovering]);
|
}, [ref, isHovering]);
|
||||||
|
|
Loading…
Reference in a new issue