I have a react component like :
import React, { PropTypes, Component } from \'react\'
class MyComponent extends Component {
componentDidMount() {
Ori's solution didn't work for me. I had to do this to make it work... (Thank you docs)
componentDidMount() {
window.addEventListener('beforeunload', this.handleLeavePage);
}
componentWillUnmount() {
window.removeEventListener('beforeunload', this.handleLeavePage);
}
handleLeavePage(e) {
const confirmationMessage = 'Some message';
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
}