mirror of
https://github.com/ThomasJockin/readexpro.git
synced 2024-09-11 10:26:46 +03:00
29 lines
717 B
JavaScript
29 lines
717 B
JavaScript
import { useRef, useState, useEffect } from 'react';
|
|
|
|
export default function useHover() {
|
|
const [value, setValue] = useState(false);
|
|
|
|
const ref = useRef(null);
|
|
|
|
const handleMouseOver = () => setValue(true);
|
|
const handleMouseOut = () => setValue(false);
|
|
|
|
useEffect(
|
|
() => {
|
|
const node = ref.current;
|
|
if (node) {
|
|
node.addEventListener('mouseover', handleMouseOver);
|
|
node.addEventListener('mouseout', handleMouseOut);
|
|
|
|
return () => {
|
|
node.removeEventListener('mouseover', handleMouseOver);
|
|
node.removeEventListener('mouseout', handleMouseOut);
|
|
};
|
|
}
|
|
},
|
|
[ref.current] // Recall only if ref changes
|
|
);
|
|
|
|
return [ref, value];
|
|
}
|