Home > Net >  React. Add/Remove event listener
React. Add/Remove event listener

Time:07-29

There are 6 posts with information about users. If the name and email in the post are too big, they are cut off. When hovering over the cropped element - a block with full information appears next to it. If registration is successful - user list is updated - newest one in the beginning. I put events onm ouseOver and onm ouseOut on items. Is it necessary to removeEventListener? Tried to do it in return UseEffect, but selected items = null [There are current elements][1]

CodePudding user response:

You speak about onm ouseOver, listener starting by "on" and which are put on tags, doesn't need a removeEventListener. It's only when you used addEventListener. In react, addEventListener isn't used with some exceptions

CodePudding user response:

Read the article about events - https://en.reactjs.org/docs/handling-events.html

You can add events using useEffect. return triggered when component unmount. Code example:

useEffect(() => {
  function handleClick() {
    console.log("click")
  }

  window.addEventListener("click", handleClick);
  return () => window.removeEventListener("click", handleClick);
}, []);
  • Related