简体   繁体   English

使用 componentDidMount 和 componentWillUnmount 在 React 中添加和删除事件监听器

[英]Add and Remove event listeners in React with componentDidMount and componentWillUnmount

componentDidMount() {
      document.addEventListener('mousemove', (e) => this.customCursorFollow(e));
      document.addEventListener('click', this.customCursorClick);
  }

  componentWillUnmount() {
      document.removeEventListener('mousemove', (e) =>
        this.customCursorFollow(e)
      );
      document.removeEventListener('click', this.customCursorClick);
  }

  customCursorFollow(e) {
    const cursor = document.querySelector('.cursor');
    cursor.setAttribute(
      'style',
      'top: ' + (e.pageY - 20) + 'px; left: ' + (e.pageX - 20) + 'px;'
    );
  }

  customCursorClick() {
    const cursor = document.querySelector('.cursor');
    cursor.classList.add('expand');

    setTimeout(() => {
      cursor.classList.remove('expand');
    }, 500);
  }

in React my addEvent listener and remove event listener aren't working properly.在 React 中,我的 addEvent 侦听器和删除事件侦听器无法正常工作。 Specifically, the removeEventListener is not working for the customCursorFollow.具体来说,removeEventListener 不适用于 customCursorFollow。

ALSO: Is this the best way to add event and remove event listeners in React?还:这是在 React 中添加事件和删除事件侦听器的最佳方式吗? with componentDidMount and componentWillUnmount?使用 componentDidMount 和 componentWillUnmount?

With addEventListener and removeEventListener , one major requirement is that the function reference passed to addEventListener should be the same the one passed to removeEventListener .对于addEventListenerremoveEventListener ,一个主要要求是传递给addEventListener的 function 引用应该与传递给removeEventListener的引用相同。

Now when you use arrow functions the reference differs and hence the listener is not removed correctly.现在,当您使用箭头函数时,引用会有所不同,因此无法正确删除侦听器。

Since you just need event to be passed you don't need a listener to be an arrow function.由于您只需要传递事件,因此您不需要将侦听器作为箭头 function。

componentDidMount() {
      document.addEventListener('mousemove', this.customCursorFollow);
      document.addEventListener('click', this.customCursorClick);
  }

  componentWillUnmount() {
      document.removeEventListener('mousemove', this.customCursorFollow);
      document.removeEventListener('click', this.customCursorClick);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否需要在componentWillUnmount中的React组件上取消绑定所有事件侦听器 - Do I need to unbind all event listeners on my React components in componentWillUnmount 反应为什么要删除事件侦听器? - React why should I remove event listeners? 在类中添加/删除事件侦听器和此上下文 - Add / remove event listeners and this context inside a class 如何添加和删除作为箭头函数的事件侦听器 - How to add and remove event listeners that are arrow functions 如何在 React 中添加被动事件侦听器? - How to add passive event listeners in React? 在我的React自定义挂钩的每次调用中添加和删除事件侦听器是否太昂贵? 怎么避免呢? - Is it too expensive to add and remove event listeners on every call of my React custom hook? How to avoid it? 在React.js中,使用componentWillUnmount删除事件监听器,如何确保事件监听器被删除? - In React.js, using componentWillUnmount to remove an event listener, how can I make sure the event listener gets removed? 删除事件监听器javascript - remove event listeners javascript 如何在 React Native 中删除事件侦听器或发射器侦听器 - How to remove event listener or emitter listeners in React Native 在反应中切换路由时如何删除事件侦听器? - How do I remove event listeners when switching route in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM