-1

The logout function should be executed only when the window is closed. How can I differentiate between refresh and close when using react-beforeunload hook?

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • Does this answer your question? [Identifying Between Refresh And Close Browser Actions](https://stackoverflow.com/questions/568977/identifying-between-refresh-and-close-browser-actions) – thisisrandy Aug 31 '22 at 04:58

1 Answers1

1

You can't.

Like the documentation says, it "listens to the beforeunload window event". As the documentation states

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.

Both closing the tab/browser AND page refresh trigger that event, so you can't rely on that event.

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107