Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Cannot read property 'addEventListener' of null #6740

Closed
xveganxxxedgex opened this issue May 10, 2016 · 1 comment
Closed

Comments

@xveganxxxedgex
Copy link

xveganxxxedgex commented May 10, 2016

I'm getting this error in the console when certain components are removed from the DOM. It's calling the method below, which assumes that target is not null and tries to access addEventListener of null object. Should it be checking if (target && target.addEventListener) {...} instead? Are components being removed in an incorrect way that would cause this to happen?

/**
 * Upstream version of event listener. Does not take into account specific
 * nature of platform.
 */
var EventListener = {
  /**
   * Listen to DOM events during the bubble phase.
   *
   * @param {DOMEventTarget} target DOM element to register listener on.
   * @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
   * @param {function} callback Callback function.
   * @return {object} Object with a `remove` method.
   */
  listen: function (target, eventType, callback) { // target = null, eventType = "click", callback = emptyFunction()
    if (target.addEventListener) {
      target.addEventListener(eventType, callback, false);
      return {
        remove: function () {
          target.removeEventListener(eventType, callback, false);
        }
      };
    } else if (target.attachEvent) {
      target.attachEvent('on' + eventType, callback);
      return {
        remove: function () {
          target.detachEvent('on' + eventType, callback);
        }
      };
    }
  }
@gaearon
Copy link
Collaborator

gaearon commented May 10, 2016

This is most likely a duplicate of #6538.
It has been fixed in master in #6650 and will be out in the next React release soon.

@gaearon gaearon closed this as completed May 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants