After the component is unmounted, I get errors when I resize the window. I know window.removeEventListener
is being called, but it is acting as if it never gets called. The error says:
warning.js:36 Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the HeaderMain component.
I even tried using a code example from the React docs, and it does the same thing my class was doing. From https://facebook.github.io/react/tips/dom-event-listeners.html:
import React from "react";
var HeaderMain = React.createClass({
getInitialState: function() {
return {windowWidth: window.innerWidth};
},
handleResize: function(e) {
this.setState({windowWidth: window.innerWidth});
},
componentDidMount: function() {
window.addEventListener('resize', this.handleResize);
},
componentWillUnmount: function() {
window.removeEventListener('resize', this.handleResize);
},
render: function() {
return <div>Current window width: {this.state.windowWidth}</div>;
}
});
module.exports = HeaderMain;
I've tried messing around with bind()
, I've tried it in ES6
, and tried different versions of React.js
. I can't get rid of the error.
How do I make sure my event listener will get removed?