0

I have tried to occur an aler <Modal /> only when browser back. But currently, default alert shows and everytime when I leave from this website an alert occurs. How can I fix this?

constructor(props) {
  super(props);

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

onUnload(event) {
  console.log("alert");
  event.returnValue = "alert";
}

componentDidMount() {
  window.addEventListener("beforeunload", this.onUnload);
}

componentWillUnmount() {
  window.removeEventListener("beforeunload", this.onUnload);
}
Shun Yamada
  • 879
  • 1
  • 10
  • 25
  • Possible duplicate of [Dialog box runs for 1 sec and disappears?](https://stackoverflow.com/questions/6063522/dialog-box-runs-for-1-sec-and-disappears) – Just code Nov 15 '18 at 05:20

1 Answers1

0

I don't use React but that would work with the code below.

history.pushState(null, null);
window.addEventListener('popstate', function(e) {
    alert('BF')
});
Shun Yamada
  • 879
  • 1
  • 10
  • 25