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.
Here is my code
componentDidMount() {
window.addEventListener("beforeunload", this.endSession);
}
componentWillUnmount() {
window.removeEventListener("beforeunload", this.endSession);
}
endSession = (e) => {
axios
.post(Globals.backendServer + "/session/end", {
sessionData: this.state.sessionData,
})
.then((res) => {})
.catch((err) => {
alert(err);
});
};