React custom event listener

冷暖自知 提交于 2020-01-14 03:38:04

问题


I'm writing a Chrome extension that places a button next to the "reply" button of each email. And elsewhere on the page is the main React component. When the component loads, i add listeners to a custom event:

  componentDidMount: function() {
    this.listenForFileEmailEvent();
  },

  listenForFileEmailEvent: function() {
    window.addEventListener("fileThisEmail", this.handleFileEmail, false);
  },

I place the button in each email and setup the event:

  $(document).on("click", ".file-this-email", function(e) {
    var email = $(e.target).parents(".gs").find('.gD').first().attr("email");
    var name = $(e.target).parents(".gs").find('.gD').first().attr("name");
    var content = $(e.target).parents(".gs").find(".adP").first().text();

    evt = new CustomEvent("fileThisEmail", {
      detail: {
        name: name,
        email: email,
        content: content
      }
    });

    window.dispatchEvent(evt);
  });

The component is placed in the conversation view of each thread. So, naturally, if you go between the inbox, and then back to a conversion, another component is inserted. And thus the old listeners aren't removed, so multiple events fire the second time.

I tried to add the following before I setup the listener:

  listenForFileEmailEvent: function() {
    window.removeEventListener("fileThisEmail", this.handleFileEmail, false);
    window.addEventListener("fileThisEmail", this.handleFileEmail, false);
  },

But when I inspect the listener events after removing, and prioring to adding it again, it is still there.

So I think there are 2 questions, is there a better way to do this for a rogue button that will live outside the component? And if not, how can I ensure multiple events don't fire for this?


回答1:


I think you'll find that this.handleFileEmail isn't necessarily the same function since you are adding/removing the component.

Try removing the listener in componentWillUnmount instead.

componentWillUnmount: function() {
  window.removeEventListener("fileThisEmail", this.handleFileEmail, false);
}


来源:https://stackoverflow.com/questions/26143612/react-custom-event-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!