I'm new to React hooks, I'm just wondering why the addEventListener is undefined even though I used it inside the useEffect
.
function useHover() {
const ref = useRef();
const [hovered, setHovered] = useState(true);
const enter = () => setHovered(true);
const leave = () => setHovered(false);
useEffect(() => {
ref.current.addEventListener('mouseenter', enter);
ref.current.addEventListener('mouseleave', leave);
return () => {
ref.current.removeEventListener('mouseenter', enter);
ref.current.removeventListener('mouseleave', leave);
};
}, [ref]);
return [ref, hovered];
}`enter code here`
getHashSet :
You can nest those commands in the componentDidMount/componentWillMount and that should stage those listeners, but that’s not a best practice. \n\nHere’s the docs I follow when I handle events.\n\nurl: https://reactjs.org/docs/handling-events.html",
2020-03-22T16:10:29