react 为元素添加自定义事件监听器

Posted chenkeyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react 为元素添加自定义事件监听器相关的知识,希望对你有一定的参考价值。

https://zhenyong.github.io/react/tips/dom-event-listeners.html

class MovieItem extends React.Component {

  componentDidMount() {
    // When the component is mounted, add your DOM listener to the "nv" elem.
    // (The "nv" elem is assigned in the render function.)
    this.nv.addEventListener("nv-enter", this.handleNvEnter);
  }

  componentWillUnmount() {
    // Make sure to remove the DOM listener when the component is unmounted.
    this.nv.removeEventListener("nv-enter", this.handleNvEnter);
  }

  // Use a class arrow function (ES7) for the handler. In ES6 you could bind()
  // a handler in the constructor.
  handleNvEnter = (event) => {
    console.log("Nv Enter:", event);
  }

  render() {
    // Here we render a single <div> and toggle the "aria-nv-el-current" attribute
    // using the attribute spread operator. This way only a single <div>
    // is ever mounted and we don‘t have to worry about adding/removing
    // a DOM listener every time the current index changes. The attrs 
    // are "spread" onto the <div> in the render function: {...attrs}
    const attrs = this.props.index === 0 ? {"aria-nv-el-current": true} : {};

    // Finally, render the div using a "ref" callback which assigns the mounted 
    // elem to a class property "nv" used to add the DOM listener to.
    return (
      <div ref={elem => this.nv = elem} aria-nv-el {...attrs} className="menu_item nv-default">
        ...
      </div>
    );
  }

}

 

以上是关于react 为元素添加自定义事件监听器的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Vue 自定义指令中添加事件监听器?

HTML5Canvas 内部元素添加事件处理

HTML5Canvas 内部元素添加事件处理

React 事件机制

React Hooks:确保 useEffect 仅在自定义钩子的数组参数内容更改时运行的惯用方法

vue使用自定义指令监听Dom元素宽高变化