How to call an API on before unload event in react, I have tried fetch with Keepalive true & sendbecon(doesn't support authorization headers in headers)
Also, I want to show dialog box with custom message on unload. the behavior is in consistent.
// Handle browser tab close or refresh
useEffect(() => {
const handleBeforeUnload = (e) => {
e.preventDefault();
e.returnValue = '';
// unlock user API call
unlock();
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => window.removeEventListener('beforeunload', handleBeforeUnload);
}, []);