Home:ALL Converter>React Hooks addEventListener undefined

React Hooks addEventListener undefined

Ask Time:2020-03-23T00:04:29         Author:cachess

Json Formatter

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`

Author:cachess,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60801623/react-hooks-addeventlistener-undefined
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
yy