title: onbeforeunload not working inside react component
body: <p>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 case of unsaved changes.</p>
  
  <p>In the component's constructor I call:</p>
  
  <pre><code>window.addEventListener("beforeunload", this.handleWindowBeforeUnload);
  </code></pre>
  
  <p>I also tried</p>
  
  <pre><code>window.onbeforeunload = this.handleWindowBeforeUnload;
  </code></pre>
  
  <p>The <code>handleWindowBeforeUnload</code> looks like this:</p>
  
  <pre><code>private handleWindowBeforeUnload = (ev: BeforeUnloadEvent): string =&gt; {
      return "Unsaved changes. Are you sure?";
  }
  </code></pre>
  
  <p>However, setting a breakpoint will hit. But nevertheless there is no prompt showing that leaving may be dangerous. Also tried with latest Firefox but nothing happened.
  As stated on MDN I also tried</p>
  
  <pre><code>// Cancel the event as stated by the standard.
  e.preventDefault();
  
  // Chrome requires returnValue to be set.
  e.returnValue = '';
  
  // return something to trigger a dialog
  return null; // ''; // "Do something"
  </code></pre>
  
  <p>Still nothing happens.
  What am I doing wrong here?</p>