title: Axios call in beforeunload function only works when refreshing (ReactJS)
body: <p>I need to let the server know when the user closes or reloads the page. I'm listening to the "beforeunload" event and when the function is called I'm sending an axios call to the server. It works when refreshing the page but not when closing it and I don't know how to fix it.</p>
  
  <p>Here is my code</p>
  
  <pre><code>  componentDidMount() {
      window.addEventListener("beforeunload", this.endSession);
    }
  
    componentWillUnmount() {
      window.removeEventListener("beforeunload", this.endSession);
    }
  
    endSession = (e) =&gt; {
      axios
        .post(Globals.backendServer + "/session/end", {
          sessionData: this.state.sessionData,
        })
        .then((res) =&gt; {})
        .catch((err) =&gt; {
          alert(err);
        });
    };
  </code></pre>