Cannot read property 'removeEventListener' of null -Vue issue?
See original GitHub issueHere is a fiddle to see my problem. https://jsfiddle.net/45t8umsp/ (After adding the keys, go to home and then to /foo. The destroy method fails)
In the event of beforeDestroy
I want clean up the instance with:
beforeDestroy() {
this.placesInstance.destroy();
},
However, I’m not quite sure, it fails with the error:
TypeError: Cannot read property 'removeEventListener' of null
at EventEmitter.placesInstance.destroy (app.js:229670)
at VueComponent.beforeDestroy (app.js:229445)
at callHook (vendor.js:3348)
at VueComponent.Vue.$destroy (vendor.js:3127)
at destroy (vendor.js:4605)
at invokeDestroyHook (vendor.js:6170)
at VueComponent.patch [as __patch__] (vendor.js:6508)
at VueComponent.Vue.$destroy (vendor.js:3150)
at destroy (vendor.js:4605)
at invokeDestroyHook (vendor.js:6170)
If I’m aspecting the code, I see the error line:
autocompleteContainer.querySelector(".".concat(prefix, "-input")).removeEventListener('input', inputListener);
But I checked in beforeDestory event the input is still there…
What is wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to get over Cannot read property 'removeEventListener ...
The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null. The...
Read more >[SOLVED] Cannot Read Property 'addEventListener' of Null in ...
To fix the “cannot read property 'addEventListener' of null” error, make sure the selector used the access the element is properly defined.
Read more >Cannot read property 'addEventListener' of Null in JS
The error "Cannot read property addEventListener of null" occurs when we call the addEventListener() method on a DOM element that doesn't exist.
Read more >Cannot read property 'removeEventListener' of undefined
Hi, I have the same issue, the error occurs after I do a local query. But the error is not caught by the...
Read more >Uncaught TypeError cannot read property 'addeventlistener' of ...
In JavaScript, a very common error is the Uncaught TypeError Cannot read property 'addeventlistener' of null. This error occurs when JavaScript is not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is the solution: places.js (and autocomplete.js too) modify the DOM to add the container around the input, which makes Vue lose track of the elements, and removes the input, before the
beforeDestroy
hook is called. The way to go is to make an input with vanilla JS inside a container (so Vue doesn’t control that part at all). The destroying works as expected.Note however that if you add any listeners yourself, you need to make sure that you remove them in the beforeDestroy hook as well
@Haroenv Thank you!