I have a simple react-component where a user can edit data. As the values that may be changed could take some time I want to ask the user to confirm when leaving the page in
You'll need to call the method inside the lifecycle methods:
componentDidMount() { window.addEventListener("beforeunload", this.handleWindowBeforeUnload); } componentWillUnmount() { window.removeEventListener("beforeunload", this.handleWindowBeforeUnload); }