ReactJS

From eBabel wiki
Jump to: navigation, search

Warning: 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 undefined component.

This error means there is probably a memory leak in your code because of a component that has been unmounted but hasn't cleaned its references, so that your code still refer to it and builds up a memory leak.

The solution is to use componentWillUnmount to clean up references that shouldn't be kept going when a component is unmounted (single page app that brings components in and out of the app).

Example

export default class Chat extends Component {
  componentWillMount() {
    this.server = this.props.server;
    this.server.on('message', this.logMessage.bind(this));
  }
  
  componentWillUnmount() {
    this.server.removeAllListeners('message');
  }
}

Note: this.server is a socket.io client-side object.