Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import { useEffect, useState } from "react"; import type { RefObject } from "react"; const useHover = <T extends HTMLElement = HTMLElement>( elementRef: RefObject<T> ): boolean => { const [value, setValue] = useState<boolean>(false); const handleMouseEnter = () => { setValue(true); }; const handleMouseLeave = () => { setValue(false); }; useEffect(() => { const el = elementRef.current; el?.addEventListener("mouseenter", handleMouseEnter); el?.addEventListener("mouseleave", handleMouseLeave); return () => { el?.removeEventListener("mouseenter", handleMouseEnter); el?.removeEventListener("mouseleave", handleMouseLeave); }; }, [elementRef]); return value; }; export default useHover; |