I am trying to send a get request to express from a react client when the user closes the tab.
I actually managed it with this, but with no confirmation:
window.addEventListener('beforeunload', async () => await axios('/delete');
If I try to make use of the proper confirmation message:
window.addEventListener('beforeunload', (e) => {
e.preventDefault();
e.returnValue = '';
return true;
})
I do get the confirmation message, but I have no way to capture the response to run the function only if leave is clicked.
I saw a lot of responses about this but they are very old and dont seem to work on the latest browsers.