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

Cannot read property addEventListener of null #6679

Closed
gsabran opened this issue May 2, 2016 · 3 comments
Closed

Cannot read property addEventListener of null #6679

gsabran opened this issue May 2, 2016 · 3 comments

Comments

@gsabran
Copy link

gsabran commented May 2, 2016

I've a component whose render method looks like

render() {
    const {condition} = this.state;

    if (!condition) {
      return null;
    }

    return <div onClick={() => { doSomething(); }} >
      ...
    </div>
  }

When the state changes and condition changes from true to false, this triggers the error

EventListener.js:37 Uncaught TypeError: Cannot read property 'addEventListener' of null
Here is the trace if that helps:


EventListener.listen    @   EventListener.js:37
SimpleEventPlugin.didPutListener    @   SimpleEventPlugin.js:612
EventPluginHub.putListener  @   EventPluginHub.js:113
putListener @   ReactDOMComponent.js:175
_assign.notifyAll   @   CallbackQueue.js:66
ON_DOM_READY_QUEUEING.close @   ReactReconcileTransaction.js:79
Mixin.closeAll  @   Transaction.js:202
Mixin.perform   @   Transaction.js:149
Mixin.perform   @   Transaction.js:136
_assign.perform @   ReactUpdates.js:89
flushBatchedUpdates @   ReactUpdates.js:165
ReactUpdates_flushBatchedUpdates    @   ReactPerf.js:66
Mixin.closeAll  @   Transaction.js:202
Mixin.perform   @   Transaction.js:149
ReactDefaultBatchingStrategy.batchedUpdates @   ReactDefaultBatchingStrategy.js:63
batchedUpdates  @   ReactUpdates.js:97
ReactEventListener.dispatchEvent    @   ReactEventListener.js:150

Returning an empty div instead of null fixes the issue:

render() {
    const {condition} = this.state;

    if (!condition) {
      return <div></div>;
    }

    return <div onClick={() => { doSomething(); }} >
      ...
    </div>
  }
@aweary
Copy link
Contributor

aweary commented May 2, 2016

@gsabran can you a share a JSFiddle reproducing the error?

@jimfb
Copy link
Contributor

jimfb commented May 2, 2016

Probably a duplicate of #6538

@gsabran
Copy link
Author

gsabran commented May 2, 2016

Sounds like a duplicate. Sorry about that. I could not get a simple JSFiddle to reproduce... Feel free to close it.

@jimfb jimfb closed this as completed May 2, 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

3 participants