react icon indicating copy to clipboard operation
react copied to clipboard

React 15 update: Uncaught TypeError: Cannot read property 'addEventListener' of null

Open omerts opened this issue 10 years ago • 32 comments

Hello,

After updating to React 15.0.1, we are getting the following error:

Uncaught TypeError: Cannot read property 'addEventListener' of null

image

omerts avatar Apr 18 '16 11:04 omerts

Curious: What browser+version are you using when you see this error? Any chance it's Chrome 49? Can you reproduce on Firefox or any other browser?

jimfb avatar Apr 18 '16 12:04 jimfb

Also, can you provide a simple jsfiddle that demonstrates the issue?

jimfb avatar Apr 18 '16 12:04 jimfb

@jimfb Browser: chrome Version: 46.0.2490.80

It also reproduces on firefox developer version 47.0a2 (2016-04-18) image

It is a big application we have. I will try to create a small fiddle for reproducing.

omerts avatar Apr 18 '16 13:04 omerts

+1, I also saw this annoying message output every time after adopting React v15.

I found that typeError can be removed by deleting onClick event in div and is able to reproduce every time.

<div ... onClick={ onClick }></div>

to

<div ... ></div>

It happened on Chrome 49.0 and Firefox 48 (it threw TypeError: target is null) and Safar 9.1i.

rickychien avatar Apr 20 '16 05:04 rickychien

@rickychien @omerts If someone could try to produce a minimal repro / jsfiddle, that would be very helpful!

jimfb avatar Apr 20 '16 05:04 jimfb

@rickychien @omerts Another question, are you guys using ReactRouter and/or Redux?

jimfb avatar Apr 20 '16 16:04 jimfb

We're seeing the same issue and are using both ReactRouter and Redux.

"react": "15.0.1",
"react-dom": "15.0.1",
"react-redux": "4.4.1",
"react-router": "2.3.0",
"redux": "3.3.1",

We see this even if we switch back to React 0.14.8, so I don't think it is specific to React 15.

We can only reproduce the issue by switching pages via react-router Links, not using browser back/forward buttons, so it's possible this is actually a react-router issue. Removing the onClick listener on the button fixes the issue for us as well.

Weirdly, our application has tons of other buttons with onClick listeners which get removed on route change which do not cause this error, so I'm having trouble figuring out how to repro.

chelzwa avatar Apr 20 '16 17:04 chelzwa

Posted a Codepen over in #6371

chelzwa avatar Apr 20 '16 19:04 chelzwa

We are having this issue as well, both with 15 and 0.14.8 so I don't think its related to the newest release.

We started having this issue once we added a componentWillReceiveProps method to a stateful component. Once we removed this function, the error no longer occurs.

Our core dependencies are:

"react": "^15.0.1", 
"react-dom": "^15.0.1",
"react-redux": "^4.4.0",
"redux": "^3.3.1"

bencripps avatar Apr 22 '16 14:04 bencripps

I trimmed it down to a case that involves just React.

Fiddle: https://jsfiddle.net/ycLw3bbq/

var callbacks = [];
function emitChange() {
  callbacks.forEach(c => c());
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { showChild: true };
  }
  componentDidMount() {
    this.setState({ showChild: false });
  }
  render() {
    return (
      <div>
        <ForceUpdatesOnChange />
        {this.state.showChild && <EmitsChangeOnUnmount />}
      </div>
    );
  }
}

class EmitsChangeOnUnmount extends React.Component {
  componentWillUnmount() {
    emitChange();
  }
  render() {
    return null;
  }
}

class ForceUpdatesOnChange extends React.Component {
  componentDidMount() {
    this.onChange = () => this.forceUpdate();
    this.onChange()
    callbacks.push(this.onChange);
  }
  componentWillUnmount() {
    callbacks = callbacks.filter(c => c !== this.onChange)
  }
  render() {
    return (
      <div key={Math.random()} onClick={function(){}} />
    )
  }
}

ReactDOM.render(<App />, document.getElementById('container'));

gaearon avatar Apr 22 '16 17:04 gaearon

Also, a few observations:

  1. This only happens with onClick listeners which get queued here for Mobile Safari.
  2. It seems related to batching. In the initial reproduction cases, I still used a <Link>, and I noticed that navigating inside setTimeout instead didn’t repro the problem, but wrapping it in unstable_batchedUpdates did. In my small repro above, I don’t use events or unstable_batchedUpdates, but it seems like doing this stuff inside the initial render batch has the same effect, so I’m calling forceUpdate and setState in componentDidMount.

gaearon avatar Apr 22 '16 18:04 gaearon

Replacing

  render() {
    return (
      <div key={Math.random()} onClick={function(){}} />
    )
  }

with

  render() {
    return (
      <img key={Math.random()} />
    )
  }

reproduces #6371 (Uncaught Invariant Violation: Must be mounted to trap events).

gaearon avatar Apr 22 '16 18:04 gaearon

I guess the issue here is that all transaction.getReactMountReady().enqueue calls in mountComponent seem to imply that unmountComponent is guaranteed to happen after getReactMountReady() callbacks have happened, but this may break if one of those callbacks can cause children component to unmount.

I think the sequence is:

  1. Composite parent enqueues its componentDidMount in transaction.getReactMountReady()
  2. DOM component mounts
  3. DOM component enqueues DOM-related in transaction.getReactMountReady().enqueue
  4. transaction.getReactMountReady() starts calling its callbacks
  5. The composite’s componentDidMount was registered first, and it queues an update to the child which causes the DOM component to unmount
  6. DOM component unmounts
  7. transaction.getReactMountReady() proceeds and calls the unmounted DOM component’s callbacks

gaearon avatar Apr 22 '16 18:04 gaearon

Yet another manifestation of the same issue (this time, only in master).

  render() {
    return (
      <input key={Math.random()} />
    )
  }

gives Uncaught TypeError: Cannot read property 'valueTracker' of null

gaearon avatar Apr 22 '16 18:04 gaearon

I won’t be looking into this more for now as I’m not sure how to proceed. I think @spicyj might have more context here.

This is a visualization of what happens. Note how div(4)#reactMountReadyCallback happens after div(4)#unmount.

screen shot 2016-04-22 at 19 45 34

gaearon avatar Apr 22 '16 18:04 gaearon

#6371 was reported earlier and these two errors are caused by the same underlying problem so I propose to close this one in favor of #6371. @jimfb?

gaearon avatar Apr 24 '16 02:04 gaearon

+1 I have the same error since i upgraded to 15

laurentlaporte avatar Apr 26 '16 16:04 laurentlaporte

As per @gaearon, yes, this is a duplicate of #6371 so we'll close in favor of that.

jimfb avatar Apr 29 '16 06:04 jimfb

Just upgraded from 0.14.7 to 15.0.2 to get the fix for #3762. I'm rendering to tbody and got "Invariant Violation: findComponentRoot(..., .0): Unable to find element. " when navigating with a React Router <Link>. After upgrading, I instead get this "Cannot read property 'addEventListener' of null" error.

I will see if I can create a test case, but wanted to note it here for the record, since it seems like the bugs are related.

jacobrask avatar Apr 30 '16 18:04 jacobrask

I just landed a fix for this (#6650) which will be in 15.0.3 or 15.1.0, whichever we release next.

sophiebits avatar May 01 '16 06:05 sophiebits