minutemailer / react-popup

React popup component
http://minutemailer.github.io/react-popup/
MIT License
208 stars 71 forks source link

Warning: setState(...): Can only update a mounted or mounting component. #22

Closed isaac-manubag closed 7 years ago

isaac-manubag commented 7 years ago

On Initial load of a Page where Popup is used - I do not get this error. When I try to load a different page and go back to this page where Popup is used I get the error

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 Popup component.

This happens to me with a very basic usage of the Popup component

import React, {Component} from "react";

class CreatePurchaseOrder extends Component {
  constructor(props) {
    super(props);

    this.showPopup = this.showPopup.bind(this);
  }

  showPopup() {
    Popup.alert('hey');

  }

  render() {
    return (
      <div>
        <Popup
          className="mm-popup"
          btnClass="mm-popup__btn"
          closeBtn={true}
          closeHtml={null}
          defaultOk="Ok"
          defaultCancel="Cancel"
          wildClasses={false} />

        <section className="content">
          <button onClick={this.showPopup}> Show Popup </button>
        </section>
      </div>
    )
  }
}
isaac-manubag commented 7 years ago

However the warning message does not really break the the project that I am trying to build or break the functionality of the Popup component. It would just be a nice thing to fix these warnings for sanity :)

tbleckert commented 7 years ago

Ah yes, I'm guessing that CreatePurchaseOrder unmounts when you switch page and sometime after that you're trying to close the popup (or do something else that causes a state change in the Popup component).

First off we should of course prevent this from happening event though, as you said, this is just a warning. But I would also suggest mounting the popup component globally so that it's always mounted when you need it. It's also safer to have it as close to the root as possible considering styling of the component.

There's of course lots of use cases for a component like this and we're working on ways to make it more flexible and work in more different scenarios.

Anyways, thanks for using it and reporting issues, very appreciated. And if you have time for it feel free to submit PRs.

isaac-manubag commented 7 years ago

@tbleckert thanks for looking into this. Yes mounting it to a more global context has fixed the warnings for me. Closing this issue now 👍